【发布时间】:2013-07-22 04:43:17
【问题描述】:
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);
}
谁能告诉我为什么这个哈希方法设计成这样?有什么好处?
【问题讨论】:
-
这段代码来自哪里?输入值是多少?
-
是java的HashMap的方法。输入值是对象的哈希码
-
所以它是散列一个散列?我不认为这是有道理的......
-
@Brad 除非你不相信你得到的哈希值。看看 Integer.hashCode() 并考虑一个小地图它只需要低 4 位。
-
顺便说一句,您可以将此扩展到其他机器人大小。第一行将位划分为 3 个稍微不等的部分,第二行将此范围划分为 3。这意味着一个位可以切换 9 位结果。