HashSet如何检查两个对象?

问题描述 投票:-1回答:1
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")
java hash
1个回答
-1
投票

由于您的HashSet应该为HashSet<TestA>,所以您甚至无法编译将TestB实例作为参数编译到contains中。

© www.soinside.com 2019 - 2024. All rights reserved.