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));
}
returns:
false
true
Per https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#contains-java.lang.Object-, HashSet uses equal to judge if it contains an element. So shouldn't the contains call return true here? What am I missing? Thanks.