In various jdk1.7 jdk1.8 HashMap and the underlying source code embodied and implemented Detailed

First, what hashcode that?

In the underlying source implementation of HashMap, we often see hashcode, then the hashcode what in the end is?

Whether in jdk1.7 or 1.8, will be used when calculating the hash value method. It is a native method Object class, the method may be understood as a bottom, mainly for Returns the hash code which is stored in the object heap address jvm concerned. I.e. not override this method, different objects are different from the value obtained, the following source code:

 public native int hashCode();

Second, what hash functions are?

1.jdk1.7 hash functions:

We can see from source hash value is XORed with hashcode value, and by a series of resulting shifted out of operation, so the hash can be obtained with good hash value

2.jdk1.8 hash functions

Can be seen from the number of rows, the number of jdk1.8 hash function simplifies its use hashcode value calculated hash value and a shift operation

Third, how to determine the hashmap in the array subscript

1. Let's look at the put function jdk1.7

Determining the value of the array index i is determined by the hash function and indexFor, while the function is used indexFor & hash value calculated from the length of the array and -1
Here Insert Picture Description
Here Insert Picture Description

2.jdk1.8's put function

jdk1.8 putVal called in function, and wherein the processing similar to 1.7, also determined length of the array, with the -1 & hash value calculated over the array subscript
Here Insert Picture Description

Fourth, why use the XOR (^) operator in the calculation of hash values ​​do not (&) operator to do?

When calculating the hash value of the hash value, the more we ought to ensure better hash Imagine if the & operator will be how? As long as the bit is 0, then the operator carried out & results obtained on the site will certainly be 0, then it will lead to a part number is not eligible, so the hash of will decline.

V. Why & (length-1) to obtain an array subscript

We know, in the capacity of the HashMap is a power of two, then converted to a binary 0 is definitely the last one, then the length-1 is able to ensure that the binary is high is 0, 1 are low (such as 16: 0001 0000 (16-1): 0000 1111) and so can be bit of the hash value hashcode low, so that the entire array, the entire memory array using more objects, reduce hash conflicts. And the use of &, calculated to ensure the label does not cross-border, because the upper length of the array are converted into a binary 0, after & calculation must also obtain high numbers 0, which ensures that the array is not out of range.

Sixth, why the shift operation?

The shift is to allow high hashcode is also involved in computing, so that the entire hash better results.

Seven, then the problem again, as so left brains of hashcode hash value, why not use hashcode directly involved in the operation to calculate the array subscript, and also with hashcode value calculated hash value and then to ask array mark it?

Do not forget, HashMap capacity is not her unchangeable, when the HashMap expansion, we need to take full advantage of the new array to the space, which is required to elements of the array redistribution due to hashcode value related to the storage address , as long as the elements change, then their position in the array will not change, while adding to the array length to calculate the hash value, it is time to let the expansion, the capacity of the array changes, the hash value will change, and then elements array position will also change again.

to sum up:

+ jdk1.7HashMap underlying implementation uses an array list, the underlying implementation using an array jdk1.8 + (chain length of 8 when turned red-black tree) + red-black tree list
jdk1.7HashMap used when adding the list is the first interpolation method, jdk1.8 interpolation with the tail

Published 24 original articles · won praise 0 · Views 609

Guess you like

Origin blog.csdn.net/weixin_43896829/article/details/104375274