Java SE 052 HashSet and HashMap source code in-depth analysis

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 052 HashSet and HashMap source code in-depth analysis

1. HashSet bottom source code analysis

1.1. The bottom layer of HashSet is implemented using HashMap.

1.2. How HashSet uses HashMap

Explanation:
  (1) When we put an element in the Set, it actually uses the element as the key of the Map maintained at the bottom, and the value is the same object.

  (2) When using the add method to add an object to the Set, the object is actually used as the key of the underlying Map object maintained, and the value is the same Object object (we don’t use this object.)

  (3) When the HashMap is constructed, it usually first generates an Entry type array with a length of 16.

1.3. The concept of Hash table

  (1) When an object is placed in an array, an operation is performed with the object to be placed through the hash function. After this operation, a position is obtained, and this position is used as the position that should be placed in the array.

  (2) If you want to find the location of this element, you can also use the hash (hash) function to perform another operation, and it will directly locate the location of the element, but there will be a conflict situation, one of mine There are already elements. When I add elements, it may happen to be at this position again. Because there are elements, there is no way to put them inside, so it will provide a function to hash and go again. Calculate the address. That is, the calculation method of notifying the re-hashing will place the element in a new position. If you hash and find that there is a new element at this position, it will not recalculate it. It will think that the elements of this array are almost full. At this time, it will open up a new array and put the original array in The elements of are hashed into the new array. At this time, the free space of the array increases a bit, and then put elements in it, the probability of conflicts is reduced. This conflicting term is called collision. When do we think that the array is almost full, it is determined by default_load_factor (the load factor). That is, it thinks that when 75% of the positions in the array are occupied by elements, the array is full. Will open up a new array.

  (3) default_initial_capacity: indicates the length of the initial array. That is, an Entry type array with a length of 16 is generated. So where is our object placed? It is placed in the array. That is, the bottom layer of HashMap is obviously implemented with an array. The bottom layer of HashSet is HashMap, and the bottom layer of HashMap is array.

1.4 HashMap bottom layer

  The bottom layer of HashMap maintains an array, and the objects we place in HashMap are actually stored in the array.

1.5.Entry<k,v> content

  The entry type array maintained at the bottom of HashMap, each element is an Entry type, and each Entry type is responsible for maintaining a key information and a value information. In addition to these two, there is also a reference to Entry<K,V> next, which is a reference of type Entry<K,V>.

Insert picture description here

1.6.Map object

  (1) The bottom layer is indeed an array, and each array element type is an Entry type.

  (2) Each Entry type is continuously linked down by the structure of a linked list, and in each Entry, the key information and value information added to the Map are implemented in this way. Array plus linked list is such a mixed structure.

  (3) After all, so many collections are done through arrays. Because only arrays can maintain some objects. Nothing else can be maintained. No matter how complex and simple the structure is, the bottom layer is nothing more than these two methods.

1.7. Summary

  (1) When putting a pair of key values ​​into the HashMap, it will calculate a position based on the hashCode value of the key, which is the position where the object is going to be stored in the array.

  (2) If there is no object at this position, put this object directly into the array; if there is already an object at this position, start searching along the chain of the existing object (the Entry class has a next member of Entry type Variable, pointing to the next object of the object), if there is an object on the chain, then use the equals method to compare, if the equals method of an object on the chain compares to false, put the object in In the array, then link the object that previously existed at that position of the array to the back of this object.

1.8. HashMap memory implementation layout diagram:

Insert picture description here

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108540933