先看下java源码中对hashcode()方法里面用到的变量的声明。
/** The value is used for character storage. */ private final char value[];//定义一个字符数组value,用于存储字符串里面的字符 /** The offset is the first index of the storage that is used. */ private final int offset;//定义offset变量表示字符串第一个字符的下标索引 /** The count is the number of characters in the String. */ private final int count;//定义count变量表示字符串中字符的个数 /** Cache the hash code for the string */ private int hash; // Default to 0,默认值为0
/** * Returns a hash code for this string. The hash code for a * <code>String</code> object is computed as * <blockquote><pre> * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] * </pre></blockquote> * using <code>int</code> arithmetic, where <code>s[i]</code> is the * <i>i</i>th character of the string, <code>n</code> is the length of * the string, and <code>^</code> indicates exponentiation. * (The hash value of the empty string is zero.) * * @return a hash code value for this object. */ public int hashCode() { int h = hash; if (h == 0 && count > 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; }
下面以cat为例。根据hashcode()源码中的循环部分的算法,计算一下cat的hashcode值
首先,c,a,t 的ascii码值分别为99,97,116;字符个数3,所以循环部分的代码变成如下所示
public int hashCode() { int h = hash; if (h == 0 && count > 0) { int off = 0; char val[] = value; int len = 3; for (int i = 0; i < 3; i++) { h = 31*h + val[off++]; } hash = h; } return h; }
也就是总共3次循环
第一次循环:h1=31*0+val[0]=val[0]='c'=99
第二次循环:h2=31*h1+val[1]=31*99+'a'=31*99+97=3166
第三次循环:h3=31*h2+val[2]=31*3166+'t'=31*3166+116=98262
所以cat的hashcode值为98262
再用多项式的表示方法检验一下s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
99*31的平方+97*31+116结果也等于98262
所以计算正确。