Java hashCode() 方法
hashCode() 方法用於返回字串的哈希碼。
字串對象的哈希碼根據以下公式計算:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
使用 int 演算法,這裏 s[i] 是字串的第 i 個字元,n 是字串的長度,^ 表示求冪。空字元串的哈希值為 0。
語法
public int hashCode()
參數
- 無。
返回值
返回對象的哈希碼值。
實例
實例
public class Test {
public static void main(String args[]) {
String Str = new String("www.xuhuhu.com");
System.out.println("字串的哈希碼為 :" + Str.hashCode() );
}
}
public static void main(String args[]) {
String Str = new String("www.xuhuhu.com");
System.out.println("字串的哈希碼為 :" + Str.hashCode() );
}
}
以上程式執行結果為:
字串的哈希碼為 :321005537