(Special Edition) HashMap in hash (Object key) principle, why (hashcode >>> 16).

We all know (jdk1.8) HashMap array index is calculated HashMap core algorithms. Xiao Bian today to see the hash (Object key) method in the source code to see HashMap baffled. Xiao Bian asked Baidu, find related blog, even HashMap English explanation of hash (Object key) are read. But just as even as possible in order to say, did not speak in detail. Xiao Bian today as we explain in detail what these two issues.

The HashMap hash (Object key) principle, why do it?
Look under the hash (Object key) method, basically everyone can understand in detail, but know this step (h = key.hashCode ()) ^ (h >> people> 16) very little reason.

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

First, this method's return value is a hash value. Why not return key.hashCode () do? Also with (h >>> 16) XOR. You must first know the following points:

Essential knowledge: ^ operator >>> calculation & operation.

1. h >>> what 16 is that what is the use?
H is hashcode. >>> h 16 h are high for removing 16, (unsigned shift right >>>) show the following:

0000 0100 1011 0011  1101 1111 1110 0001
 
>>> 16 
 
0000 0000 0000 0000  0000 0100 1011 0011

2. Why h = key.hashCode ()) and (h >>> 16) XOR
mentioned herein a method depends indexFor, there indexFor (int h in jdk1.7 in, int length) method. There is no jdk1.8, but the principle has not changed. Look below 1.7 source code

1.8 with tab [(n - 1) & hash] in place but the same principle.

static int indexFor(int h, int length) {
    return h & (length-1);
}

This method returns a value is an array subscript. We usually there is not much use map data in most cases under the map. Here the (length-1) & phase,

However, since the vast majority of cases the length is generally less than 2 ^ 16 that is less than 65,536. Therefore return h & (length-1); h the result is always the low 16 bits (length-1) & calculation performed. The following examples (four bytes hashCode)

Example 1: In order to facilitate verification of length 8 is assumed. Default capacity is 16 HashMap

length = 8; (length-1) = 7; 111 converted to binary;

Assuming a conversion key hashcode = 78897121 binary is: 100101100111101111111100001, and (length-1) & calculates the following

    0000  0100  1011  0011  1101  1111  1110  0001
  
& operational 
 
    0000  0000  0000  0000  0000  0000  0000  0111
  
=    0000  0000  0000  0000  0000  0000  0000  0001 (decimal is 1, so the subscript 1)

Essence of the calculation are: 001 and 111 operation. That is, three low and length of the hash value calculation. If you let the low three more random hash value, then the result is even more random & how to make low three more random hash value, then it is allowed with high XOR.

Additional knowledge:

When the lower three length = 8 standard operation result depends on the hash value

When the lower four standard length = 16 results in the calculation depends on the hash value

When the low five standard length = 32 depending on the result of calculation of the hash value

When the N bits of the N-th power length = 2, the subscript operation result depends on the hash value.

3. Cause summary
due and (length-1) operation, the vast majority of length 16 is less than 2 to the power. So hashcode is always lower 16 bits (or even lower) involved in computing. If the high 16-bit operation also involved, you will get more hash index.

So this is less than the high 16's, 16 how high it is also involved in computing. So have hash (Object key) method. Let his hashCode () and its own high 16 ^ operator. Therefore, (h >>> 16) gets his 16 with a high hashCode () ^ operation.

4. Why instead of ^ & and |
since & and | will make the results biased towards zero or one, not even the concept, so use ^.

That's why there are hash (Object key) is.



Guess you like

Origin www.cnblogs.com/yuexiaoyun/p/12158914.html