Java
[고급자바] equals(), hashcode()
elog
2023. 3. 3. 06:00
1. Person 클래스
class Person {
private int num;
private String name;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
return Objects.hash(name, num);
}
// @Override
// public boolean equals(Object obj) {
// if (this == obj) { // 참조값이 같으면
// return true;
// }
//
// if (obj == null) {
// return false;
// }
//
// if (this.getClass() != obj.getClass()) {
// return false;
// }
//
// Person that = (Person) obj; // 매개변수의 값의 현재 객체 유형으로 형변환 한다.
//
// if (this.name == null && that.name != null) {
// return false;
// }
//
// if (this.num == that.num && this.name == that.name) {
// return true;
// }
//
// if (this.num == that.num && this.name.equals(that.name)) {
// return true;
// }
//
// return 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;
return Objects.equals(name, other.name) && num == other.num;
}
} // Person 클래스
2. 테스트 클래스
Person p1 = new Person();
p1.setNum(1);
p1.setName("홍길동");
Person p2 = new Person();
p2.setNum(1);
p2.setName("홍길동");
Person p3 = p1;
System.out.print("p1 p2 : ");
System.out.println(p1 == p2);
System.out.print("p1 p3 : ");
System.out.println(p1 == p3);
System.out.print("p2 p3 : ");
System.out.println(p2 == p3);
System.out.println("p1 p2 참조값 : " + p1.equals(p2));
System.out.println("p1 p3 참조값 : " + p1.equals(p3));
System.out.println("p2 p3 참조값 : " + p2.equals(p3));
HashSet<Person> testSet = new HashSet<>();
testSet.add(p1);
testSet.add(p2);
testSet.add(p3);
System.out.println("Set의 갯수 : " + testSet.size());
System.out.println("p1 : " + p1.hashCode());
System.out.println("p2 : " + p2.hashCode());
System.out.println("p3 : " + p3.hashCode());