Why return (h = key.hashCode()) ^ (h >>> 16) other than key.hashcode?

Viewed 3160

I don't see this approach avoid the collision. I think if the key.hashcode is larger than table.length, there will be collision.

Updates: Actually I refer to the HashMap#hash in JDK 1.8, and was a little confused about the benifit of spread the higher bits downward. Now, I think I'm clear with the help of this link, the benifits is:

  • We don't need to do the % calculation, but using a more faster way - bit shift.

For the collision, if the number of key is larger than the length of the table, then there will be a collision no matter what hash method is used.

2 Answers
Related