# 为什么String选择数字31作为hashCode方法乘子?

1. 背景

## 2. 选择数字31的原因

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, information would be lost, as multiplication by 2 is equivalent to shifting. The advantage of using a prime is less clear, but it is traditional. A nice property of 31 is that the multiplication can be replaced by a shift and a subtraction for better performance: 31 * i == (i << 5) - i. Modern VMs do this sort of optimization automatically.

As Goodrich and Tamassia point out, If you take over 50,000 English words (formed as the union of the word lists provided in two variants of Unix), using the constants 31, 33, 37, 39, and 41 will produce less than 7 collisions in each case. Knowing this, it should come as no surprise that many Java implementations choose one of these constants.

## 3. 实验及数据可视化

### 3.2 哈希值分布可视化

3作为乘子时，算出的哈希值分布情况和2很像，只不过稍微好了那么一点点。从图中可以看出绝大部分的哈希值最终都落在了第32分区里，哈希值的分布性很差。这个也没啥用，拖出去枪毙5分钟吧。在看看数字17的情况怎么样：

# 往期精彩

mysql binlog应用场景与原理深度剖析

TCP粘包、拆包与通信协议详解

08-23 367
09-04 11