在我阅读JDK的源代码之后,我发现HashMap的hash()功能看起来很有趣.它的官方代码如下:
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
Run Code Online (Sandbox Code Playgroud)
参数h是的hashCode从Objects它投入HashMap.这种方法如何工作?为什么?为什么这个方法可以抵御糟糕的hashCode函数?