Boolean.hashCode()

2022-01-19 00:00:00 boolean java hashcode

Boolean类的hashCode()方法是这样实现的:

The hashCode() method of class Boolean is implemented like this:

public int hashCode() {
    return value ? 1231 : 1237;
}

为什么使用 1231 和 1237?为什么不是别的?

Why does it use 1231 and 1237? Why not something else?

推荐答案

1231 和 1237 只是两个(足够大)任意素数.任何其他两个大素数都可以.

1231 and 1237 are just two (sufficiently large) arbitrary prime numbers. Any other two large prime numbers would do fine.

为什么是素数?
假设我们选择了合数(非素数),比如 1000 和 2000.当将布尔值插入哈希表时,true 和 false 将进入桶 1000 % N resp 2000 % N(其中N是桶的数量).

Why primes?
Suppose for a second that we picked composite numbers (non-primes), say 1000 and 2000. When inserting booleans into a hash table, true and false would go into bucket 1000 % N resp 2000 % N (where N is the number of buckets).

现在请注意

  • 1000 % 82000 % 8
  • 相同的桶
  • 1000 % 102000 % 10
  • 相同的桶
  • 1000 % 202000 % 20
  • 相同的桶
  • ....

换句话说,它会导致许多冲突.

这是因为 1000 的因式分解 (23, 53) 和 2000 的因式分解 (24, 53) 有很多共同因素.因此选择素数,因为它们不太可能与桶大小有任何共同因素.

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.

为什么大素数.2和3不行吗?
在计算复合对象的哈希码时,通常会为组件添加哈希码.如果在具有大量存储桶的哈希集中使用的值太小,则可能会导致对象分布不均.

Why large primes. 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.

碰撞重要吗?布尔值只是有两个不同的值吗?
地图可以包含布尔值和其他对象.此外,正如 Drunix 所指出的,创建复合对象散列函数的常用方法是重用子组件散列代码实现,在这种情况下,最好返回大素数.

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.

相关问题:

  • 为什么在 hashCode 中使用素数?
  • 什么是哈希码计算的明智素数?
  • 为什么Java的hashCode() 在 String 中使用 31 作为乘数?

相关文章