我有一个(不可变的)对象 Group,我想在 HashSet 中使用它。但是,我得到了奇怪的结果:
// Position is another immutable class, GroupType is an enum
Group t1 = new Group(new Position(0, 0), GroupType.ROW);
Group t2 = new Group(new Position(0, 0), GroupType.ROW);
Set<Group> s = new HashSet<Group>();
s.add(t1);
System.out.format("t1.hashCode(): %d\nt2.hashCode(): %d\nt1.hashCode() == t2.hashCode(): %b\nt1.equals(t2): %b\nt2.equals(t1): %b\ns.contains(t1): %b\ns.contains(t2): %b\n",
t1.hashCode(),
t2.hashCode(),
t1.hashCode() == t2.hashCode(),
t1.equals(t2),
t2.equals(t1),
s.contains(t1),
s.contains(t2)
);
结果如下:
t1.hashCode(): 486656595
t2.hashCode(): 486656595
t1.hashCode() == t2.hashCode(): true
t1.equals(t2): true
t2.equals(t1): true
s.contains(t1): true
s.contains(t2): false
t1 和 t2 具有相同的哈希码,并且 equals() 声称它们相同。 HashSet 怎么可能只包含一个而不包含另一个?
(不,这些方法都没有 secret 修改 t1 或 t2;重复打印语句会获得相同的结果。)
Group.equals() 如下:
public boolean equals(Group g2) {
return (this.type.equals(g2.type)) && (this.basis.equals(g2.basis));
}
type 是存储的(最终)枚举。 basis 是一个 Position,它具有以下等于:
public boolean equals(Position pos) {
return (x == pos.x) && (y == pos.y);
}
其中 x 和 y 是内部最终变量。
但是,我得到了相同的结果,将其替换为:
public boolean equals(Group g2) {
return true;
}
最佳答案
我怀疑...我的猜测是您的 equals 方法如下所示:
public boolean equals(Group other)
这并没有覆盖 HashSet
将使用的内置 equals 方法。确保你的 equals 方法是:
@Override // Make the compiler check we're overriding something
public boolean equals(Object other)
另一种测试方法是:
Object o1 = t1;
Object o2 = t2;
System.out.println(o1.equals(o2));
System.out.println(o2.equals(o1));
关于java - 对象相等,散列码相等,HashSet 包含一个但不包含另一个---如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5382793/