What I've done in the below program is, I've created a TreeSet named "alls" with a custom comparator which compares two list and if they are equal it returns 0 else returns 1. But if I add two same list into TreeSet then TreeSet accepts two same list into it. But it should not contain two same list cuz i've defined a comparator like that.
List<Integer> a1 = new ArrayList<Integer>() {{add(1);add(2);add(3);add(4);}};
List<Integer> a2 = new ArrayList<Integer>() {{add(1);add(1);}};
List<Integer> a3 = new ArrayList<Integer>() {{add(2);add(1);}};
List<Integer> a4 = new ArrayList<Integer>() {{add(3);add(1);}};
List<Integer> a5 = new ArrayList<Integer>() {{add(4);add(1);}};
List<Integer> a6 = new ArrayList<Integer>() {{add(5);add(1);}};
Comparator b1 = (l1,l2)->{if(l1.equals(l2)) return 0; else return 1;};
Collection<List<Integer>> alls = new TreeSet(b1);
alls.add(a1);
alls.add(a1);
alls.add(a1);
alls.add(a2);
alls.add(a3);
alls.add(a1);
alls.add(a4);
alls.add(a6);
alls.add(a5);
alls.add(a2);
alls.add(a1);
When I try to Print my TreeSet(name alls) then the following output shows up:
1 2 3 4
1 1
2 1
1 2 3 4
3 1
5 1
4 1
You can see [1 2 3 4] is inserted into my TreeSet(whose name is alls) twice but it does not get inserted after.
How is this possible? I though TreeSet with my custom comparator doesn't allow duplicates. Also if it allows why not the same elements doesn't get inserted further in my program