Java – why does HashMap insert new nodes on the index (n-1) and hash?

Why does HashMap insert a new node on the index

Where hash = key hashCode()^ key. Hashcode() > > > 16 and N = tab of the array of nodes < K, V > length.

Why doesn't HashMap place node: tab [hash] like this? Is it just another hash function, such as multiplying 31 in most hashcode () methods? Thank you for your explanation!

Solution

Because the hash may be out of range

The "canonical solution" is to use the array length to obtain the (positive) modulus of the hash. This code uses the fact that the array has a power length of 2 to replace the expensive modulus (the modulus is a). Using cheap bitwise and can well optimize the constant

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>