The c.remove() method removes the Collection entry by value, not by index, as per method definition in the Collection interface - Collection.remove(Object o). Hence the c.remove(3); line would remove the first occurrence of an object equivalent to Integer(3).
An ArrayList is an unsorted Collection which allows duplicates, hence the output will be [2, 1, 2]
A LinkedHashSet is an unsorted Collection which does not allow duplicates, hence the output will be [2, 1].
A TreeSet is a sorted collection, which does not allow duplicates. The elements in a TreeSet are sorted in ascending order (unless a custom comparator is specified), hence the output will be [1, 2].
Войдите чтобы поставить Нравится
Войдите чтобы прокомментировать