HashMap's expansion mechanism principle

version 1.7

(1) First generate a new array
(2) Traverse each element of the linked list at each position in the old array
(3) Take the key of each element, and calculate the location of each element in the new array based on the length of the new array
(4) Add elements to the new array
(5) After transferring the elements, assign the new array to the table property of the HashMap object .

version 1.8

(1) First generate a new array
(2) Traverse the linked list or red-black tree at each position in the old array
(3) If it is a linked list, recalculate the subscript of each element in the linked list and add it to the new array
(4) If it is a red-black tree, first traverse the red-black tree, and first calculate the corresponding position of each element in the red-black tree in the new array: a. Count the
number of elements at each subscript position
b. If If the number of elements at this position exceeds 8, a new red-black tree is generated and the root node is added to the corresponding position
c of the new array. If the number of elements at this position does not exceed 8, a linked list is generated and the linked list The head node of the new array is added to the corresponding position of the new array
(5) After all elements are transferred, the new array is assigned to the table property of the HashMap object.

Guess you like

Origin blog.csdn.net/weixin_49131718/article/details/131677096