java.util.HashSet不遵守其规范吗?

fir*_*dle 2 java hashset

作为一个相对的Java菜鸟,我很困惑,找出以下内容:

Point.java:

public class Point {
...
    public boolean equals(Point other) {
        return x == other.x && y == other.y;
    }
...
}
Run Code Online (Sandbox Code Playgroud)

Edge.java:

public class Edge {
    public final Point a, b;
    ...
    public boolean equals(Edge other) {
        return a.equals(other.a) && b.equals(other.b);
    }
...
}
Run Code Online (Sandbox Code Playgroud)

main snippet:private Set 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));
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么这令人惊讶?因为我在编码之前检查了文档以依赖于相等性,它:

如果此set包含指定的元素,则返回true.更正式地说,当且仅当此集合包含元素e时才返回true(o == null?e == null:o.equals(e))

这句话非常清楚,它表明只需要平等.f.equals(e)返回true,如输出中所示.很明显,集合确实包含元素e,使得o.equals(e),但包含(o)返回false.

虽然哈希集也取决于哈希值是否相同,这当然是可以理解的,但是在HashSet本身的文档中既没有提到这个事实,也没有在Set的文档中提到任何这样的可能性.

因此,HashSet不符合其规范.这看起来像是一个非常严重的错误.我在这里走错了路吗?或者接受这样的行为怎么样?

aio*_*obe 10

你没有压倒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;
}
Run Code Online (Sandbox Code Playgroud)

(同样Edge)

在覆盖hashCode时始终覆盖也很重要equals.请参阅例如为什么我需要覆盖Java中的equals和hashCode方法?

请注意,如果您使用过,则编译会捕获此错误@Override.这就是为什么在可能的情况下始终使用它的好习惯.