Java中如何判断两个对象是否相等(Java equals and ==)
2020-12-13 02:19
标签:就是 void 问题: alt bsp https tcl 现在 数据 原文https://www.dutycode.com/post-140.html 如何判断两个对象相等,这个问题实际上可以看做是如何对equals方法和hashcode方法的理解。 从以下几个点来理解equals和hashCode方法: Java中如何判断两个对象是否相等(Java equals and ==) 标签:就是 void 问题: alt bsp https tcl 现在 数据 原文地址:https://www.cnblogs.com/Andrew520/p/11031488.htmlpublic boolean equals(Object obj) {
return (this == obj);
}
public class EqualTest {
public static void main(String[] args) {
Person p1 = new Person(10,"张三");
Person p2 = new Person(10,"张三");
System.out.println(p1.equals(p2));
}
}
class Person{
int age;
String name;
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果我们复写equals方法呢?我们认为名字和年龄一样的就是同一个人,那么p1和p2都表示10岁的张三,这两个对象应该是相等的。复写的equals方法如下:Person未复写equals方法,则默认使用了Object中的equals,即为两个对象(p1和p2)的内存地址判断,p1和p2很明显内存地址不同,所以输出结果很明显为false。@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
BTW:如果equals方法返回true,那么==是否也是true?同样的,执行上述用例,得到的结果是true。public static void main(String[] args) {
Person p1 = new Person(10,"张三");
Person p2 = new Person(10,"张三");
System.out.println(p1.equals(p2));
System.out.println(p1 == p2);
}
1. 对称性:如果x.equals(y)返回是"true",那么y.equals(x)也应该返回是"true"。
2. 反射性:x.equals(x)必须返回是"true"。
3. 类推性:如果x.equals(y)返回是"true",而且y.equals(z)返回是"true",那么z.equals(x)也应该返回是"true"。
4. 一致性:如果x.equals(y)返回是"true",只要x和y内容一直不变,不管你重复x.equals(y)多少次,返回都是"true"。
5. 非空性,x.equals(null),永远返回是"false";x.equals(和x不同类型的对象)永远返回是"false"。
public class EqualTest {
public static void main(String[] args) {
Person p1 = new Person(10, "张三");
Person p2 = new Person(10, "张三");
System.out.println(
"p1.equals(p2)=" + p1.equals(p2) + ", p1.hashcode=" + p1.hashCode() + ", p2.hashcode=" + p2.hashCode());
}
}
class Person {
int age;
String name;
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
p1.equals(p2)=true, p1.hashcode=246688959, p2.hashcode=1457895203
从结果可以看出,equals虽然相同,但是p1和p2的hashcode并不相同。public class EqualTest {
public static void main(String[] args) {
Person p1 = new Person(10, "张三");
Person p2 = new Person(10, "张三");
System.out.println(
"p1.equals(p2)=" + p1.equals(p2) + ", p1.hashcode=" + p1.hashCode() + ", p2.hashcode=" + p2.hashCode());
HashSet
p1.equals(p2)=true, p1.hashcode=246688959, p2.hashcode=1457895203
[Person [age=10, name=张三], Person [age=10, name=张三]]
class Person {
int age;
String name;
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
}
p1.equals(p2)=true, p1.hashcode=776160, p2.hashcode=776160
[Person [age=10, name=张三]]
上一篇:springMVC:为MultipartFilte配置了上传文件解析器,报错或不能使用
下一篇:很不错的一个叫初学者做计算器的视屏http://www.56.com/redian/MTM0OTg4Mw/ODE2OTQzNjU.html
文章标题:Java中如何判断两个对象是否相等(Java equals and ==)
文章链接:http://soscw.com/essay/25342.html