Collection

A Collection in Java is any group of objects β€” a list, set, queue or deque. The root interface java.util.Collection defines the common operations: add, remove, contains, size, iterator. Map is related but not a Collection.

The hierarchy

Collection
β”œβ”€β”€ List      β†’ ArrayList, LinkedList, CopyOnWriteArrayList
β”œβ”€β”€ Set       β†’ HashSet, LinkedHashSet, TreeSet, EnumSet
└── Queue     β†’ ArrayDeque, PriorityQueue, LinkedList
    └── Deque β†’ ArrayDeque, LinkedList

Map (not a Collection)
β”œβ”€β”€ HashMap, LinkedHashMap, TreeMap, ConcurrentHashMap, EnumMap

Example

Collection<String> c = new ArrayList<>();
c.add("Ada");
c.add("Grace");
c.remove("Ada");
System.out.println(c.size());       // 1
System.out.println(c.contains("Grace")); // true
for (String s : c) System.out.println(s);

Collections utility class

The java.util.Collections class (plural!) provides static helpers:

Collections.sort(list);
Collections.reverse(list);
Collections.unmodifiableList(list);
Collections.emptyList();
Collections.singleton(item);

Factory methods (Java 9+)

List<String> list = List.of("a", "b", "c");    // immutable
Set<Integer> set  = Set.of(1, 2, 3);            // immutable
Map<String, Integer> map = Map.of("a", 1, "b", 2); // immutable