小编Clo*_*Lee的帖子

任何人都可以解释一下java设计HashMap的hash()函数吗?

在我阅读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的hashCodeObjects它投入HashMap.这种方法如何工作?为什么?为什么这个方法可以抵御糟糕的hashCode函数?

java hash hashmap

7
推荐指数
1
解决办法
2661
查看次数

标签 统计

hash ×1

hashmap ×1

java ×1