java - 在许多 GetHashCode 实现中,为什么在 xoring 之前乘以质数?

标签 java .net algorithm hash

我知道在异或之前乘以一个大数应该有助于处理分布不均的操作数,但为什么乘数应该是素数?

Related:
Why should hash functions use a prime number modulus?

Close, but not quite a Duplicate:
Why does Java’s hashCode() in String use 31 as a multiplier?

最佳答案

有一个 good article on the Computing Life blog详细讨论了这个话题。它最初是作为对我在问题中链接到的 Java hashCode() 问题的回应而发布的。根据文章:

Primes are unique numbers. They are unique in that, the product of a prime with any other number has the best chance of being unique (not as unique as the prime itself of-course) due to the fact that a prime is used to compose it. This property is used in hashing functions.

Given a string “Samuel”, you can generate a unique hash by multiply each of the constituent digits or letters with a prime number and adding them up. This is why primes are used.

However using primes is an old technique. The key here to understand that as long as you can generate a sufficiently unique key you can move to other hashing techniques too. Go here for more on this topic about hashes without primes.

关于java - 在许多 GetHashCode 实现中,为什么在 xoring 之前乘以质数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1488977/

相关文章:

java - 删除字符串中的特定字符

java - 一个简单的java客户端服务器程序

.net - 如何在 .net 中应用 XSLT 2.0 转换?

java - 将 LinkedList 值插入数据库

c# - 无法使用 Directory.Delete(path, true) 删除目录

c# - .NET BitmapSource 正在锁定文件

python - 将 IP 范围转换为 CIDR 表示法的模块、脚本或算法

algorithm - 找到从特定输入计算特定输出的算法

algorithm - 是否可以编写通用算法来使用 Zippers 更新嵌套(无论嵌套如何)数据结构中的元素?

java - 如何将Dialog转化为DialogFragment?