62,614
社区成员
发帖
与我相关
我的任务
分享
public class test {
public static void main(String[] args){
String a = "a";
System.out.println(a.hashCode());
System.out.println(Objects.hashCode("a"));
System.out.println(Objects.hash(a));
}
}
public static int hash(Object... values) {
return Arrays.hashCode(values);
}
这里调用的Arrays中hashCode()函数是这样的
public static int hashCode(Object a[]) {
if (a == null)
return 0;
int result = 1;
for (Object element : a)
result = 31 * result + (element == null ? 0 : element.hashCode());
return result;
}
从上面代码可以看出,传入参数a是,return result 的值是31*1 + a的hashCode值
就是31+97=128
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;
}
//
public static int hashCode(final Object o) {
return o == null ? 0 : o.hashCode();
}
//调用用这两个方法都是获取a的 hashcode的值 a====>97