Horner's Method 计算string的哈希值:
public int hashCode() {
int hash = 0;
for (int i = 0; i < string.length(); i++) {
hash = s[i] + (31 * hash);
}
return hash;
}计算自定义类的哈希值:
public int hashCode() {
int hash = 17;
hash = 31 * hash + reference.hashCode();
hash = 31 * hash + ((Double) primitive).hashCode();
return hash;
}Reference直接用其hashcode,primitive 用wrapper计算hashcode。
基本公式是31*x + y.
Modular Hash (Hash Table) 哈希表:
Hash Code 是32位的int值, 将其转换成0到M-1的index值。
public int hashCode(T item) {
return (item.hashCode() & 0x7fffffff) % M );
}

1468

被折叠的 条评论
为什么被折叠?



