【发布时间】:2011-12-16 20:18:25
【问题描述】:
在搜索 HashMap 实现后,在 http://www.docjar.com/html/api/java/util/HashMap.java.html 上找到了此代码。
264 static int hash(int h) {
265 // This function ensures that hashCodes that differ only by
266 // constant multiples at each bit position have a bounded
267 // number of collisions (approximately 8 at default load factor).
268 h ^= (h >>> 20) ^ (h >>> 12);
269 return h ^ (h >>> 7) ^ (h >>> 4);
270 }
有人可以解释一下吗?评论告诉我们为什么这段代码在这里,但我想了解这如何改善坏的哈希值以及它如何保证位置的数量有限碰撞。这些神奇的数字是什么意思?
【问题讨论】:
标签: java hashmap bit-manipulation hash openjdk