请看以下代码:
import java.util.*;
class A
{
public boolean equals(Object obj)
{
return true;
}
}
class B
{
public int hashCode()
{
return 1;
}
}
class C
{
public int hashCode()
{
return 2;
}
public boolean equals()
{
return true;
}
}
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());
System.out.println(books);
}
}
编译器运行是这个结果([B@1, B@1, C@2, C@2, A@18f1d7e, A@d9660d]),,而书上给出的结果是
c@2只打印了一个其他都一样,[WHy |
|