【问题标题】:Java HashSet doesn't add two objets with same hashCode and equals (OK) but contains() says second object is not in setJava HashSet 不会添加具有相同 hashCode 和等于(OK)的两个对象,但 contains() 表示第二个对象不在集合中
【发布时间】:2019-06-25 03:32:32
【问题描述】:

我有一个测试来测试添加相同的边(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;
    }
}

【问题讨论】:

    标签: java equals hashset hashcode


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      相关资源
      最近更新 更多