简体   繁体   English

高效的hashCode()实现

[英]Efficient hashCode() implementation

I often auto-generate an class's hashCode() method using IntelliJ IDEA and typically the method takes the form: 我经常使用IntelliJ IDEA自动生成类的hashCode()方法,通常该方法采用以下形式:

result = 31 * result + ...

My question is what is the purpose of multiplying by 31? 我的问题是乘以31的目的是什么? I know this is a prime number but why pick 31 specifically? 我知道这是一个素数,但为什么选择31? Also, if implementing a hashCode() for a particularly small / large dataset would people approach this problem differently? 另外,如果为特别小/大的数据集实现hashCode() ,人们会以不同的方式处理这个问题吗?

Multiplying by 31 is fast because the JIT can convert it to a shift left by 5 bits and a subtract: 乘以31是快速的,因为JIT可以将其转换为左移5位和减法:

x * 31 == (x << 5) - x

Without any particular extra information, I'd stick to this approach. 没有任何特别的额外信息,我会坚持这种方法。 It's reasonably fast and likely to end up with reasonably well-distributed hash codes, and it's also easy to get right :) 它的速度相当快,并且可能最终得到分布均匀的哈希码,而且它也很容易正确:)

The size of the dataset doesn't really matter, but if you have particular extra information about the values you'll be work with (eg "it's always even") then you may be able to design a better hash function. 数据集的大小并不重要,但如果你有关于你将使用的值的特定额外信息(例如“它总是偶数”),那么你可以设计一个更好的哈希函数。 I'd wait until it's an actual problem first though :) 我会等到第一个真正的问题但是:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM