java Boolean.hashCode()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3912303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boolean.hashCode()
提问by user471011
The hashCode()
method of class Boolean is implemented like this:
hashCode()
Boolean 类的方法是这样实现的:
public int hashCode() {
return value ? 1231 : 1237;
}
Why does it use 1231 and 1237? Why not something else?
为什么要用1231和1237?为什么不是别的?
回答by aioobe
1231 and 1237 are just two (sufficiently large) arbitrary prime numbers. Any other two large prime numbers would do fine.
1231 和 1237 只是两个(足够大的)任意素数。任何其他两个大素数都可以。
Why primes?
Suppose for a second that we picked composite numbers (non-primes), say 1000 and 2000. When inserting booleans into a hash table, trueand falsewould go into bucket 1000 % N
resp 2000 % N
(where N
is the number of buckets).
为什么是素数?
假设我们选择了合数(非质数),比如 1000 和 2000。当将布尔值插入哈希表时,true和false将进入桶1000 % N
resp 2000 % N
(其中N
是桶的数量)。
Now notice that
现在注意
1000 % 8
same bucket as2000 % 8
1000 % 10
same bucket as2000 % 10
1000 % 20
same bucket as2000 % 20
- ....
1000 % 8
相同的桶2000 % 8
1000 % 10
相同的桶2000 % 10
1000 % 20
相同的桶2000 % 20
- ....
in other words, it would lead to many collisions.
换句话说,它会导致许多碰撞。
This is because the factorization of 1000 (23, 53) and the factorization of 2000 (24, 53) have so many common factors. Thus prime numbers are chosen, since they are unlikely to have any common factors with the bucket size.
这是因为 1000 (2 3, 5 3) 的因式分解和 2000 (2 4, 5 3)的因式分解有很多公因数。因此选择素数,因为它们不太可能与桶的大小有任何公因数。
Why largeprimes. Wouldn't 2 and 3 do?
When computing hash codes for composite objects it's common to add the hash codes for the components. If too small values are used in a hash set with a large number of buckets there's a risk of ending up with an uneven distribution of objects.
为什么是大素数。2和3不行吗?
在计算复合对象的哈希码时,通常会为组件添加哈希码。如果在具有大量存储桶的散列集中使用过小的值,则可能会导致对象分布不均。
Do collisions matter? Booleans just have two different values anyway?
Maps can contain booleans together with other objects. Also, as pointed out by Drunix, a common way to create hash functions of composite objects is to reuse the subcomponents hash code implementations in which case it's good to return large primes.
碰撞重要吗?无论如何,布尔值只有两个不同的值?
地图可以包含布尔值和其他对象。此外,正如 Drunix 所指出的,创建复合对象散列函数的一种常见方法是重用子组件散列代码实现,在这种情况下,最好返回大素数。
Related questions:
相关问题:
回答by bvp
In addition to all that's said above it can also be a small easter egg from developers:
除了上面所说的之外,它也可以是来自开发人员的一个小复活节彩蛋:
true: 1231 => 1 + 2 + 3 + 1 = 7
真:1231 => 1 + 2 + 3 + 1 = 7
7 - is a lucky number in European traditions;
7 - 是欧洲传统中的幸运数字;
false: 1237 => 1 + 2 + 3 + 7 = 13
假:1237 => 1 + 2 + 3 + 7 = 13
13 (aka Devil's dozen) - unlucky number.
13(又名魔鬼打) - 不吉利的数字。