Java HashSet不会添加具有相同hashCode且等于(OK)的两个objets,但是contains()表示第二个对象不在集合中

时间:2019-06-25 03:32:32

标签: java equals hashset hashcode

我有一个测试用于测试,添加相同的Edge(Arista)但具有相同的顶点(但翻转的顺序)是相同的(这不是有向图)。

这很奇怪,因为前两个断言通过了OK(将Edge1Edge2相加将产生edges.sizes = 1,因为从理论上讲它们是相同的。)

但是当测试edges.contains(Edge2)返回false时。

为什么在测试加法(不重复添加)时可以使用,但是在测试contains()时却不起作用?

这是代码:

    @Test
public final void testAristaWithSameVerticesIsNotAddedTwice() throws Exception {
    Grafo grafo = new Grafo();
    Vertice vertice1 = new Vertice("Vertice 1");
    Vertice vertice2 = new Vertice("Vertice 2");
    grafo.agregarVertice(vertice1);
    grafo.agregarVertice(vertice2);
    Arista arista = new Arista(vertice1, vertice2, 10);
    Arista arista2 = new Arista(vertice2, vertice1, 10);
    grafo.agregarArista(arista);
    grafo.agregarArista(arista);

    assertEquals(1, grafo.getAristasQuantity());
    assertTrue(grafo.hasArista(arista));
    assertTrue(grafo.hasArista(arista2)); // fails here
}

Grafo类:

private HashSet<Arista> aristas;

public boolean hasArista(Arista arista) {
    return this.aristas.contains(arista);
}

Arista类

package entities;

public class Arista {

    protected Vertice vertice1;
    protected Vertice vertice2;
    protected int peso;

    public Arista(Vertice vertice1, Vertice vertice2, int peso) {
        this.vertice1 = vertice1;
        this.vertice2 = vertice2;
        this.peso = peso;
    }

    public Vertice getVertice1() {
        return vertice1;
    }

    public Vertice getVertice2() {
        return vertice2;
    }

    public int getPeso() {
        return peso;
    }

    public void setPeso(int peso ) {
        this.peso = peso;
    }

    public int hashCode() {
        return vertice1.hashCode() + vertice2.hashCode();
    }

    public boolean equals(Arista arista) {
        if (arista == this) {
            return true;
        }
        if ((arista.getVertice1() == this.vertice1 && arista.getVertice2() == this.vertice2)
            || (arista.getVertice2() == this.vertice1 && arista.getVertice1() == this.vertice2)) {
            return true;
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:1)

我发现equals()并没有覆盖父定义,因为定义不正确。因此没有被调用。

正确的方法是:

@Override
public boolean equals(Object object) {
    if (object instanceof Arista) {
        Arista arista = (Arista) object;
        if (arista == this) {
            return true;
        }
        if ((arista.getVertice1() == this.vertice1 && arista.getVertice2() == this.vertice2)
            || (arista.getVertice2() == this.vertice1 && arista.getVertice1() == this.vertice2)) {
            return true;
        }
    }
    return false;
}