轉載於https://blog.csdn.net/zjq_1314520/article/details/78955104
1、對於integer源碼如下:
@Override public int hashCode() { return Integer.hashCode(value); }
public static int hashCode(int value) { return value; }
可以看出value就是對應的hashcode值
2、對於String源碼如下:
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; }
可以看出其value為依次遍歷其每個字符成員,遞歸的將當前的hashcode* 31 +下一個成員對應的ascII值
eg: String s = "10"; "1" ----> 49 "0" ------->48 h = 31 * 0 + 49 h = 49 h = 31 * 49 + 48 h = 1567
Long類型源碼如下:
可以看出其值為當前值與當前邏輯右移32位之后異或得出的值
public static int hashCode(long value) {
return (int)(value ^ (value >>> 32));
}
public static int hashCode(long value) { return (int)(value ^ (value >>> 32)); }