Why should the length of the array in HashMap be designed to a power of 2?

  The premise of understanding this article is that you must have a certain understanding of the data structure, and understand the advantages and disadvantages of each data structure. Of course, if you already know that the underlying data structure of HashMap is array + linked list + red black tree, it would be better. If you still know that the length of the initialized array of hashMap is 16, and every time the capacity is expanded to twice the original length, then I can only say "You are already a qualified gangster".

 I'm a person who likes to find out. I believe that "existence is meaningful", which usually makes me a lot of money, but occasionally it is easy to fall into a dead end. OK Nonsense, go to the topic.

The following is part of the source code of HashMap in jdk1.8

Insert picture description here

Through the source code, we can see that the newly added elements of HashMap are calculated by modulo operation ((array length -1) & key hashCode) (that is, where the new element needs to be placed in the subscript position of the array)

ps: Modulus calculation will not be explained here, the friends who want to know can baidu by themselves

The following program is a simple simulation. When the length of the array is 15, 16, add the subscript position calculated by 100 elements. The hashcode corresponding to these 100 elements increases from 0-100 respectively

  public static void main(String[] args) {
            int[] length={15,16};
            for(int n : length){
                System.out.println("--------------"+n+"-----------------");
                for(int hash=0;hash<=100;hash++){
                    System.out.print((hash & n-1)+"\t");
                }
                System.out.println();
            }  
  }
复制代码

The results are as follows

Insert picture description here

It can be seen that when the length of the array is 16, 16 slots are calculated and evenly distributed at each position of the array, when the length of the array is 15, only 8 slots are calculated, and each slot is placed A two-node linked list results in 8 slots being idle. This problem is generally referred to as hash conflict, which can cause inefficient HashMap queries. When we fetch data from the map, we can directly extract the corresponding element through the slot calculated by the key. Now because this slot stores a linked list, then we must traverse the linked list to get the data. In the case of (the hashcode of all elements are the same), HashMap will degenerate into a linked list. This loses such a feature that the array random search is efficient.

Therefore, making the length of the array equal to the second power can effectively reduce the probability of hash collision.

HashMap also has many features. If you are interested, you can refer to JDK to write a HashMap yourself. ps: 1.7 HashMap is relatively simple, if you want to study the HashMap source code, it is recommended to start with jdk1.7

Finally, attach a simple HashMap blog.csdn.net/qq_39914581…

Guess you like

Origin juejin.im/post/5e970f8d518825739837c70b