Source code analysis HashMap (super detailed graphic source code analysis)

Foreword:

I believe you must have had such problems. Some things have been used for a long time. What are they doing? I will use it but I don't know why I use it. Today I will talk about this HashMap.

text:

When we need to store data, what are the known dynamic arrays?

Although these arrays can be automatically expanded, the initial capacity must be specified at the initial moment. For those data whose specific quantity cannot be determined at compile time, that is, dynamic growth, Java collection classes are needed: ArrayList and LinkedList, as well as Vector, etc...

For ArrayList, LinkedList, and Vector, they all have some shortcomings, either insertion or deletion is slow, or traversal is slow.

So is there a collection class that is good for insertion, deletion, and traversal? So HashMap appeared.

HashMap is a hash table, which stores a set of key-value pairs and realizes quick search.

Features:

  • In order to achieve fast search, HashMap chose an array instead of a linked list. The search efficiency of O(1) complexity is realized by using the index of the array.

  • In order to search by index, HashMap introduces the Hash algorithm to map the key into an array subscript: key -> Index.

  • The introduction of the Hash algorithm has led to Hash conflicts. In order to resolve Hash conflicts, HashMap uses the chain address method to switch to linked list storage at the conflict location.

  • Too many nodes stored in the linked list cause the deterioration of the search performance of the nodes on the linked list. In order to optimize the search performance, HashMap turns the linked list into a red-black tree after the length of the linked list exceeds 8, in order to improve the search efficiency of O(n) complexity to O(log n).

Low-level implementation:

Before JDK1.8: The bottom layer of HashMap is array + linked list, which is the hash of linked list. HashMap obtains the hash value after the hashCode of the key is processed by the perturbation function, and then judges the location where the current element is stored through (n-1) & hash. If there is an element in the previous position, judge the element and the element hash to be stored Whether the value and key are the same, if they are the same, they are directly covered, if they are not the same, the conflict will be resolved through the zipper method.

After JDK1.8: When the length of the linked list is greater than the threshold (default is 8), the linked list is converted into a red-black tree to reduce the search time.

Source code comparison:

JDK 1.8

static final int hash(Object key) {
 int h;
 // key.hashCode():返回散列值也就是hashcode
 // ^ :按位异或
 // >>>:⽆符号右移,忽略符号位,空位都以0补⻬
 return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
 }

 JDK 1.7

static int hash(int h) {
 // This function ensures that hashCodes that differ only by
 // constant multiples at each bit position have a bounded
 // number of collisions (approximately 8 at default load factor).
 h ^= (h >>> 20) ^ (h >>> 12);
 return h ^ (h >>> 7) ^ (h >>> 4);
}

The underlying data structure:

                           JDK1.7

Zipper method: Combine linked list and array to solve the problem of Hash conflict. That is to say, create a linked list array, each cell in the array is a linked list, if you encounter a hash conflict, just add the conflicting value to the linked list.

                          JDK1.8 

Red-black trees are used at the bottom of TreeMap, TreeSet, and HashMap after JDK1.8. The red-black tree is to solve the defects of the binary search tree, because the binary search tree will degenerate into a linear structure in some cases, which is very inefficient.

to sum up:

The meaning of HashMap is to realize a kind of K/V (key/value) data structure with fast search and good insert and delete performance.

 

Recommended reading: Expanding information

 

 

Any restrictions start from the heart!

When you no longer endure, no longer restraint, you will truly mature!

When doing anything, you must have firm and clear goals, and keep your goals in mind!

 

            

 

       The editor needs your attention! Your praise is my greatest encouragement!

Guess you like

Origin blog.csdn.net/l_mloveforever/article/details/111356670