I created an IntPair
class as following
class IntPair {
public int x;
public int y;
public IntPair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if(this == o)
return true;
if(!(o instanceof IntPair))
return false;
IntPair p = (IntPair) o;
return x == p.x && y == p.y;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
}
When I try something like the following
System.out.println(Arrays.asList(new IntPair(1,2), new IntPair(1,2)).stream()
.distinct()
.collect(Collectors.toList())
);
I get as result a list of two IntPairs, that is
[(1, 2), (1, 2)]
even if those two pairs are equal. Why?