下面都是從String類的源碼中粘貼出來的
1 PRivate int hash; // Default to 0 2 public int hashCode() { 3 int h = hash; 4 if (h == 0 && value.length > 0) { 5 char val[] = value; 6 for (int i = 0; i < value.length; i++) { 7 h = 31 * h + val[i]; 8 } 9 hash = h;10 }11 return h;12 }下面利用上述方法計算字符串”Lee”的哈希值
‘L’的ASCII碼為76,’e’的ASCII碼為101
for循環3次
所以字符串”Lee”的哈希碼就是76268
新聞熱點
疑難解答