标签:就是 void 问题: alt bsp https tcl 现在 数据
原文https://www.dutycode.com/post-140.html
如何判断两个对象相等,这个问题实际上可以看做是如何对equals方法和hashcode方法的理解。
从以下几个点来理解equals和hashCode方法:
public 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; } }
@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; }
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
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<Person> set = new HashSet<Person>(); set.add(p1); set.add(p2); System.out.println(set); } } 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; } @Override public String toString() { return "Person [age=" + age + ", name=" + name + "]"; } }
p1.equals(p2)=true, p1.hashcode=246688959, p2.hashcode=1457895203 [Person [age=10, name=张三], Person [age=10, name=张三]]
p1和p2的equals相同,我们认为是两个对象相等,但是这两个对象竟然同时出现再hashSet中,hashSet中是不会出现两个相同的元素的。
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=张三]]
于是看到set中仅有一个Person值了。
Java中如何判断两个对象是否相等(Java equals and ==)
标签:就是 void 问题: alt bsp https tcl 现在 数据
原文地址:https://www.cnblogs.com/Andrew520/p/11031488.html