简体   繁体   English

在 Java 中 String hashCode() 的旧实现中跳过字符背后的想法是什么

[英]What is the idea behind skipping chars in the old impl of String hashCode() in Java

What is the idea of skipping some characters from a String in old versions of Java's String hashCode() implementation:在 Java 的 String hashCode() 实现的旧版本中从 String 中跳过一些字符的想法是什么:

public int hashCode() {
   int hash = 0;
   int skip = Math.max(1, length()/8);
   for (int i = 0; i < length(); i += skip)
      hash = (hash * 37) + charAt(i);
   return hash;
}

In the current version there is no skipping and the prime number is 31 instead of 37在当前版本中没有跳过,质数是 31 而不是 37

Probably to fast up the hashCode() computation but as consequence it had more potential collisions.可能是为了加快hashCode()计算,但结果是它有更多潜在的冲突。
The new version favors less collisions but requires more computations.新版本有利于减少碰撞,但需要更多的计算。

But in the facts, String s are immutable, so in more recent versions of hashCode() , that is computed once :但事实上, String是不可变的,所以在hashCode()更新版本中,它被计算一次:

public int hashCode() {
    int h = hash; 
    if (h == 0 && value.length > 0) {
        hash = h = isLatin1() ? StringLatin1.hashCode(value)
                              : StringUTF16.hashCode(value);
    }
    return h;
}

So in a some way it makes sense to favor this way as it reduces the collision number and not skipping some characters in the hashCode() computation is not so expensive as the result is cached.因此,在某种程度上支持这种方式是有意义的,因为它减少了冲突次数,并且在hashCode()计算中不跳过某些字符不会因为结果被缓存而昂贵。

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

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