As I know HashSet is not adding duplicate variable, but if I have the code:
public class A {
int i, j;
public A(int i, int j) {
this.i = i;
this.j = j;
}
public static void main(String[] args) {
Set<A> s = new LinkedHashSet<>();
s.add(new A(3,1));
s.add(new A(1,3));
s.add(new A(3,1));
s.add(new A(3,1));
s.add(new A(2,1));
System.out.println(s.size());
}
To make sure to not duplicate the variable when I add them to s
, I need to override HashCode
and equals
, but I want to know how Set is working when he adds some variables to the list? What did he check to verify that the Object are not duplicate?