【发布时间】:2017-03-30 11:04:50
【问题描述】:
我正在完成 Hashmap 的实现,并在下面找到了计算哈希码的代码。能否请您解释一下下面如何计算 h 的值以及为什么以这种特定方式完成?
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);
}
【问题讨论】: