class A{
public boolean equals(Object o) {
return true;
}
}
class B{
public int hashCode() {
return 1;
}
}
class C{
public int hashCode() {
return 2;
}
public boolean equals(Object o) {
return true;
}
}
//如果需要把某个类的对象保存到HashSet集合中,重写这个类的equals()方法时,应该尽量保证
//两个对象返回true的同时它们的hashCode方法返回值相等
public class HashSetTest {
public static void main(String[] args) {
HashSet books = new HashSet<>();
books.add(new A());
books.add(new A());
books.add(new B());
books.add(new B());
books.add(new C());
books.add(new C());
books.forEach(System.out::println);
}
}