class Node{
int x, y, value;
Node(int x, int y, int v) {
this.x = x;
this.y = y;
value = v;
}
@Override
public boolean equals(Object o) {
Node n = (Node)o;
boolean result = this.x == n.x && this.y == n.y && this.value == n.value;
return result;
}
}
void test() {
HashSet<Node> s = new HashSet<>();
Node n2 = new Node(1, 1, 11);
Node n1 = new Node(1, 1, 11);
s.add(n1);
System.out.println(s.contains(n2));
System.out.println(n1.equals(n2));
}
kehrt:HashSet sagt es kein Element enthält, obwohl gleich true zurück
falsch
wahr
Per https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#contains-java.lang.Object- verwendet HashSet gleich zu beurteilen, ob es enthält ein Element. Sollte der contains call also hier nicht wahr sein? Was vermisse ich? Vielen Dank.
Sie müssen 'hashCode()' auch überschreiben. – Eran