我有几百万个字符串,每个都是几千字节。我正在寻找一个快速、无碰撞的Java哈希函数。谁能帮帮我?有没有这样的哈希函数,不是为了密码学,只是为了性能?有什么建议吗?
你用这个作为密钥
static class Key {
private static final int R = 256;
private static final long Q = longRandomPrime();
private String k;
public Key(String k) {
this.k = k;
}
public String key() {
return k;
}
@Override
public int hashCode() {
return Long.hashCode(hash());
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null)
return false;
if (getClass() != o.getClass())
return false;
Key other = (Key) o;
return k.equals(other.k);
}
@Override
public String toString() {
return k;
}
private long hash() {
long h = 0;
for (int j = 0; j < k.length(); j++) {
h = (R * h + k.charAt(j)) % Q;
}
return h;
}
private static long longRandomPrime() {
BigInteger prime = BigInteger.probablePrime(31, new Random());
return prime.longValue();
}
}