HashSet – hashCode()의 오버라이딩 조건
package ch11_컬렉션_프레임워크2;
import java.util.*;
public class ex13_HashSet_객체 {
public static void main(String[] args) {
HashSet set = new HashSet();
// Set set = new HashSet();
set.add("abc");
set.add("abc");
set.add(new Person("David",10));
set.add(new Person("David",10));
System.out.println(set); // David가 두개 나옴
}
}
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name +":"+ age;
}
}
package ch11_컬렉션_프레임워크2;
import java.util.*;
public class ex14_HashSet_객체_hashcode {
public static void main(String[] args) {
HashSet set = new HashSet();
set.add(new String("abc"));
set.add(new String("abc"));
set.add(new Person2("David",10));
set.add(new Person2("David",10));
System.out.println(set);
}
}
class Person2 {
String name;
int age;
Person2(String name, int age) {
this.name = name;
this.age = age;
}
public boolean equals(Object obj) {
if(obj instanceof Person2) {
Person2 tmp = (Person2)obj;
return name.equals(tmp.name) && age==tmp.age;
}
return false;
}
public int hashCode() {
return Objects.hash(name, age);
}
public String toString() {
return name +":"+ age;
}
}