开发者社区 问答 正文

Hashmap的hash()函数

在jdk7版本的hashmap下有这么一个函数:

final int hash(Object k) {
    int h = hashSeed;
    if (0 != h && k instanceof String) {
        return sun.misc.Hashing.stringHash32((String) k);
    }

    h ^= k.hashCode();

    // 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);
}

下面这段代码的作用?

h ^= (h >>> 20) ^ (h >>> 12);
    return h ^ (h >>> 7) ^ (h >>> 4);

展开
收起
蛮大人123 2016-03-11 14:56:03 2235 分享 版权
1 条回答
写回答
取消 提交回答
问答分类:
问答地址: