In main method I create two different equal objects.
private static Set<Cards> allCombs = new HashSet<>();
cards = new Cards();
cards.add(new Card(1, 10));
cards.add(new Card(1, 12));
Cards cards2 = new Cards();
cards2.add(new Card(1, 10));
cards2.add(new Card(1, 12));
System.out.println(cards.toString() + " " + cards2.toString() + " " + (cards == cards2) + " " + cards.equals(cards2));
allCombs.add(cards);
allCombs.add(cards2);
System.out.println(allCombs);
My expectations is that set allCombs
has one element because two objects of Cards are equal. I checked it and DEBUG is from equals
method and objects are equal (comparison from println
). But they are not same reference...
Why add
method from HashSet
does not call Cards
's equals method while adding element?
DEBUG 6_QT 6_QT true
6_QT 6_QT false true
[6_QT, 6_QT]