我有一个(不可变的)对象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)
);
Run Code Online (Sandbox Code Playgroud)
结果如下:
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
Run Code Online (Sandbox Code Playgroud)
t1和t2具有相同的哈希码,equals()声称它们是相同的.HashSet如何包含一个而不包含另一个?
(不,这些方法都没有秘密修改t1或t2;重复print语句会获得相同的结果.)
Group.equals()如下:
public boolean equals(Group g2) {
return (this.type.equals(g2.type)) …Run Code Online (Sandbox Code Playgroud)