【发布时间】:2019-10-30 01:28:08
【问题描述】:
我正在寻找 java 中的哈希函数,它对于具有相同长度的键返回相同的哈希。
int index1 = hashmap.hash("xxx");
int index2 = hashmap.hash("yyy");
assertEquals(index1, index2);
我正在使用这个功能:
public int hash(String x) {
int hashCode = x.hashCode();
return (int) ( ( Math.abs( hash_a * hashCode + hash_b) % p_prime ) % capacity );
}
【问题讨论】:
-
hash_a、hash_b、capacity和p_prime是什么? -
不能只取字符串的长度作为hash吗?