作为一个相对的 Java 菜鸟,我很困惑地发现以下内容:
点.java:
public class Point {
...
public boolean equals(Point other) {
return x == other.x && y == other.y;
}
...
}
Edge.java:
public class Edge {
public final Point a, b;
...
public boolean equals(Edge other) {
return a.equals(other.a) && b.equals(other.b);
}
...
}
主要片段: 私有(private)设置blockedEdges;
public Program(...) {
...
blockedEdges = new HashSet<Edge>();
for (int i = 0; ...) {
for (int j = 0; ...) {
Point p = new Point(i, j);
for (Point q : p.neighbours()) {
Edge e = new Edge(p, q);
Edge f = new Edge(p, q);
blockedEdges.add(e);
// output for each line is:
// contains e? true; e equals f? true; contains f? false
System.out.println("blocked edge from "+p+"to " + q+
"; contains e? " + blockedEdges.contains(e)+
" e equals f? "+ f.equals(e) +
"; contains f? " + blockedEdges.contains(f));
}
}
}
}
为什么这令人惊讶?因为我在编码之前检查了文档以依赖于相等性,并且它 says :
Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e))
这句话说得很清楚,只需要平等就可以了。 f.equals(e) 返回 true,如输出所示。很明显,该集合确实包含一个元素 e,使得 o.equals(e),但 contains(o) 返回 false。
虽然哈希集也依赖于相同的哈希值当然是可以理解的,但 HashSet 本身的文档中没有提到这一事实,Set 的文档中也没有提到任何这种可能性。
因此,HashSet 不遵守其规范。对我来说这看起来是一个非常严重的错误。我在这里完全走错路了吗?或者说为什么这样的行为会被接受?
最佳答案
您没有重写equals
(您重载它)。 equals
需要接受 Object
作为参数。
做类似的事情
@Override
public boolean equals(Object o) {
if (!(o instanceof Point))
return false;
Point other = (Point) o;
return x == other.x && y == other.y;
}
(与 Edge
相同)
当您覆盖 equals
时,始终覆盖 hashCode
也很重要。例如,参见Why do I need to override the equals and hashCode methods in Java?
请注意,如果您使用了@Override
,则编译会捕获此错误。这就是为什么尽可能使用它是一种好的做法。
关于java - java.util.HashSet 是否不遵守其规范?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30725903/