Hash of HashMap of Java source code

When it comes to hashing, there is a way that we will immediately flash in our heads, that is hashCode(), yes. This is it!
We know that HashMap stores data through the structure of array + linked list. If there is an array, there will be an index. The index of the data in the HashMap is hashcalculated based on the internal method of the HashMap .
We  get put cannot do without this  hash method.
Let's start with the hashmethod!

static final int hash(Object key) {
    int h;
    return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

First hashCode() get it through the  method  h , and then shift the h unsigned by 16 bits to the right, and finally get the hash value through the XOR algorithm. Let's find a key to expand the operation.
Suppose there is a key

String key = "hello world";

Then the binary value of the hashCode of this key is
0110 1010 1110 1111 1110 0010 1100 0100
and then move this binary data to the right unsigned 16 bits, and get the following
0000 0000 0000 0000 0110 1010 1110 1111 and
finally by doing the high 16 bits and the low 16 bits The XOR operation is uniformly distributed
0110 1010 1110 1111 1000 1000 0010 1011 The
XOR operation for the hash is mainly to reduce the probability of hash conflicts (this is learned by consulting the data)

After we have finished hashthe calculation process, let’s take a look at where the HashMap is useful.

public V get(Object key) {
    Node<K,V> e;
    return (e = getNode(hash(key), key)) == null ? null : e.value;
}
public boolean containsKey(Object key) {
    return getNode(hash(key), key) != null;
}
public V put(K key, V value) {
    return putVal(hash(key), key, value, false, true);
}

These three methods can be regarded as the three most commonly used methods in HashMap. You can see that all the keys passed in from the application layer have been passed. Those hash(key)who are more careful will also find that the index of the array in the HashMap is through i = ( n-1) & hash calculated

Let's expand the index calculation formula
n is the length of the hash table in HashMap, the default is 16, each expansion is double the expansion, which is 16 << 1, we use 16 to expand the operation, and Hash uses the above key to continue the calculation

i = (16 - 1) & hash

The binary result of 15 is
0000 0000 0000 0000 0000 0000 0000 1111

Take the hash and use it again~ (decimal: 1794082859)
0110 1010 1110 1111 1000 1000 0010 1011

Through & operation, you can get (decimal: 11)
0000 0000 0000 0000 0000 0000 0000 1011

Decimal calculation:
1794082859 & 15 = 11
1794082859% 16 = 11

First of all, the setting of n in HashMap is always the power of 2, because the power of 2 & hash(key) is equivalent to hash(key)% n
. As we all know, & is 10 times more efficient than %.
But this formula only holds when n is the power of 2, so there is the setting of the power of 2.

The indexes of HashMap are all obtained by this algorithm, so HashMap is very fast to find~

Well, through this article, everyone should also have a little idea about the hash of HashMap.

Guess you like

Origin blog.csdn.net/a159357445566/article/details/108601854