HashMap put method

The implementation process of the put method of HashMap can be understood through the following figure. If you are interested, you can compare the source code to study more clearly.

①. Judge whether the key value is empty or null for the array table [i]. Otherwise, execute resize() to expand the capacity;

②. Calculate the hash value according to the key value to the inserted array index I. If table [i] = = null, directly create a new node and add it, and turn to ⑥. If table [i] is not empty, turn to ③;

③. Judge whether the first element of table [i] is the same as key. If it is the same, it directly overwrites value. Otherwise, turn to ④. The same here refers to hashcode and equals;

④. Judge whether table [i] is a treenode, that is, whether table [i] is a red black tree. If it is a red black tree, directly insert key value pairs into the tree, otherwise turn to ⑤;

⑤. Traverse the table [i] to determine whether the length of the linked list is greater than 8. If it is greater than 8, convert the linked list into a red black tree and perform the insertion operation in the red black tree. Otherwise, perform the insertion operation of the linked list; If the key is found to exist during traversal, you can directly overwrite the value;

⑥. After successful insertion, judge whether the actual number of key value pairs exceeds the maximum capacity threshold. If so, expand the capacity.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>