equals()和hashCode()方法
2018-03-16 本文已影响0人
我的女友漏气了
这两个方法都是基类Object的方法,可以被子类重写。
equals():比较两个对象的引用的堆内存地址是否相同,来判断是否是同一个对象在JVM里。
hashCode():对对象的堆内存地址做hashCode算法散列,便于查找的时候提高性能。
注:两个对象equals()方法相同,那hashCode()的值一定相同。反之,则不一定。
1、String类equals()和hashCode()方法
/** The value is used for character storage. */
private final char value[];
/** Cache the hash code for the string */
private int hash; // Default to 0
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
//这一看源码不就明白了么,这不就是两个对象的值比较么
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
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];//31人家外国人大神测试出来的,别问为什么。
}
hash = h;
}
return h;
}
然后我们想到 == 是个什么东东。栗子上


所以说 == 比较的是栈内存里面的引用变量的值,而上面equals()方法比较的是堆内存里面的对象值,不是内存值。
验证


2、我们这么重写一个对象里面的equals和hashCode()方法。
idea 有这种功能,在一个类里面按住Alt+Insert

如果不想比较id可以在后续的选项中把id去掉
@Data
class Man {
private Long id;
private String name;
private Integer age;
public Man(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Man)) {
return false;
}
Man man = (Man) o;
if (name != null ? !name.equals(man.name) : man.name != null) return false;
return age != null ? age.equals(man.age) : man.age == null;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (age != null ? age.hashCode() : 0);
return result;
}
}

