美文网首页
Java 中 String 字符串的 hashCode 计算原理

Java 中 String 字符串的 hashCode 计算原理

作者: 光剑书架上的书 | 来源:发表于2021-09-14 14:12 被阅读0次

Returns a hash code for this string. The hash code for a String object is computed as
s[0]31^(n-1) + s[1]31^(n-2) + ... + s[n-1]

源代码:

public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

相关文章

网友评论

      本文标题:Java 中 String 字符串的 hashCode 计算原理

      本文链接:https://www.haomeiwen.com/subject/rtycgltx.html