class TestA extends Foo{
private String a;
private String b;
private String c;
public int hashcode() {
// Use some inbuilt hashcode generator over a, b, c.
}
public boolean equals(Object a) {
// a, b, c instance variables should match
}
}
class TestB Extends Foo{
private String a;
private String b;
private String c;
public int hashcode() {
// Same as Test A class implementation
}
public boolean equals(Object a) {
// Same as Test A class implementation
}
}
说我将以下对象添加到HashSet中:
TestA a = new TestA("a", "b", "c")
set.add(a);
现在,如果我检查哈希集中的以下对象b,set.contains(b)
总是返回true吗?
TestB B = new TestB("a", "b", "c")
由于您的HashSet应该为HashSet<TestA>
,所以您甚至无法编译将TestB实例作为参数编译到contains中。