Collection hierarchy
The collection hierarchy is the family of related interfaces in the java.util package that describe how groups of objects behave. Lists, sets, queues and maps each have an interface, and several concrete classes implement each one.
Detailed Explanation
The Java Collections Framework separates what a container promises from how it delivers it. List is a promise: ordered, indexed, duplicates allowed. ArrayList and LinkedList are two ways of keeping that promise with different costs. You write your code against the interface and pick the implementation last.
This split is the whole point. Before Java 1.2 the standard library shipped Vector, Hashtable, Stack and Enumeration, with no common supertype. A method that wanted to accept "any list of strings" had to pick one concrete class, so switching implementations meant editing every signature. The framework fixed that by putting interfaces on top.
The shape of the hierarchy is small enough to memorise:
| Interface | Extends | Promise | Common implementations |
|---|---|---|---|
Iterable<E> | — | can be walked once, front to back | everything below |
Collection<E> | Iterable | add, remove, contains, size | — |
List<E> | Collection | positional access, duplicates allowed | ArrayList, LinkedList |
Set<E> | Collection | no duplicates | HashSet, LinkedHashSet, TreeSet |
SortedSet / NavigableSet | Set | sorted, plus range and nearest-match queries | TreeSet |
Queue<E> | Collection | insert at one end, remove at another | ArrayDeque, PriorityQueue, LinkedList |
Deque<E> | Queue | both ends usable | ArrayDeque, LinkedList |
Map<K,V> | nothing | key to value, keys unique | HashMap, LinkedHashMap, TreeMap |
Map is the part that surprises people. A map is not a Collection, because a map holds pairs and Collection methods like add(E) take a single element. You reach the Collection API through three views: keySet(), values() and entrySet(). Those views are live windows onto the same map, not copies, so removing from keySet() removes the entry from the map.
Between the interfaces and the concrete classes sit abstract skeletons: AbstractList, AbstractSet, AbstractMap. They exist so that writing your own collection means implementing two or three methods instead of twenty. You rarely touch them, but they explain why ArrayList has methods you never see in List.
Two utility classes complete the picture. Collections holds static helpers: sort, unmodifiableList, synchronizedMap, emptyList. Arrays bridges arrays and collections with Arrays.asList and Arrays.stream.
Java 9 added the factory methods List.of, Set.of and Map.of. They return compact, immutable implementations. Prefer them for constants: they use less memory than an ArrayList and they fail loudly if code tries to mutate them.
When would a learner reach for the hierarchy itself? Every time you declare a field or a parameter. Declare List<String> rather than ArrayList<String>, return Map<String, Integer> rather than HashMap<String, Integer>. The cost of the abstraction is close to zero at runtime because the JIT inlines interface calls that only ever see one implementation. The real cost is choosing the wrong implementation, which is why every collection entry in this glossary states its complexity.
Code Examples
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Hierarchy {
// Accepts anything that is a Collection: ArrayList, HashSet, ArrayDeque.
// Declaring the parameter as the interface is what makes this reusable.
static int totalLength(Collection<String> words) {
int sum = 0;
for (String w : words) { // works because Collection extends Iterable
sum += w.length();
}
return sum;
}
public static void main(String[] args) {
List<String> list = new ArrayList<>(List.of("log", "line", "log"));
Set<String> set = new TreeSet<>(list); // any Collection can seed another
System.out.println(totalLength(list)); // prints: 10
System.out.println(totalLength(set)); // prints: 7 (duplicate dropped)
System.out.println(set); // prints: [line, log]
}
}import java.util.HashMap;
import java.util.Map;
public class MapViews {
public static void main(String[] args) {
Map<String, Integer> counts = new HashMap<>();
counts.put("error", 2);
counts.put("warn", 5);
// Map does not extend Collection. The Collection API arrives via views.
for (Map.Entry<String, Integer> e : counts.entrySet()) {
System.out.println(e.getKey() + "=" + e.getValue());
}
// Views are live windows, not copies: this removes the whole entry.
counts.keySet().remove("warn");
System.out.println(counts.size()); // prints: 1
}
}Common Gotchas
`List.of` is immutable
the Java 9 factories return fixed collections. Calling `add`, `remove` or `set` throws `UnsupportedOperationException`, and passing `null` to `List.of` throws `NullPointerException`. Wrap them in `new ArrayList<>(List.of(...))` when you need a mutable copy.
`Arrays.asList` is a fixed-size view
`set` works because it writes through to the backing array, but `add` and `remove` throw `UnsupportedOperationException`. It is a view, so changing the array changes the list.
Generics vanish at runtime
type arguments are erased, so a `List<String>` and a `List<Integer>` are the same class to the JVM. Unchecked casts and raw types can put the wrong element into a collection, and you find out later at the point of use.
Some interfaces are honest about not promising order
`Set` and `Map` say nothing about iteration order. `HashSet` order can differ between JVM versions for the same input, which is why tests that assume it are fragile.