Java源码阅读之String.hashCode

    public static int hashCode(byte[] value) {
        int h = 0;
        int length = value.length >> 1;
        for (int i = 0; i < length; i++) {
            h = 31 * h + getChar(value, i);
        }
        return h;
    }

"abc"的哈希值计算(a的ASCII为97)
“a” = 97
“ab” = (97 * 31 ) + 98 = 3105
“abc” = (3105 * 31 ) + 99 = 96354

hashCode为int类型,当字符串的哈希值超过2147483647时为负数

猜你喜欢

转载自blog.csdn.net/weixin_42072543/article/details/88895742