原文:https://www.dutycode.com/post-140.html
从以下几个点来理解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 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=张三]]
|
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=张三]]
|