I'm reading "Effective Java" by Joshua Bloch now and when I read item 41 "Use overloading judiciously" I was confused because of the example used there. I tried it on computer and it indeed does what it does in the book. But I don't understand why!
Here's this example:
public class SetList {
public static void main(String[] args) {
Set<Integer> set = new TreeSet<Integer>();
List<Integer> list = new ArrayList<Integer>();
for (int i = -3; i < 3; i++) {
set.add(i);
list.add(i);
}
for (int i = 0; i < 3; i++) {
set.remove(i);
list.remove(i);
}
System.out.println(set + " " + list);
}
}
It outputs:
[-3, -2, -1] [-2, 0, 2]
I understand that list.remove() in this example should remove by index but it doesn't! After list was filled with values it was: [-3, -2, -1, 0, 1, 2]. So, when we remove 0, 1 and 2 elements we should stay with [0, 1, 2] and not with [-2, 0, 2]. What's going on there?