🗃️ Collections Framework · Intermediate

The collections hierarchy in Java

Iterable → Collection → List / Set / Queue; Map is separate.

🧩 The mysteryJava ships dozens of collection classes. Learn ONE family tree and you can predict what every single one of them can do. Ready to meet the family?

One family, one root

Think of a family tree. At the very top sits **Iterable: anything Iterable can be used in a for-each loop. Below it is Collection**, the parent of the three big branches.

Iterable          ← for-each works
 └─ Collection
     ├─ List   (ArrayList, LinkedList)
     ├─ Set    (HashSet, TreeSet)
     └─ Queue  (ArrayDeque, PriorityQueue)

Three children, three promises

Each branch is a contract. **List: ordered, indexed, duplicates allowed. Set: no duplicate elements. Queue**: elements waiting in line to be processed. Deque (double-ended queue) extends Queue.

List<String> l = List.of("a", "a"); // ok
Set<String> s = Set.of("a", "b");   // unique
Queue<String> q = new ArrayDeque<>(); // line

Map: the cousin who lives apart

A **Map holds keys mapped to values: pairs, not single elements. So it is not** a Collection and not Iterable. To loop, ask it for a view: keySet(), values() or entrySet(). HashMap and TreeMap live in this separate tree.

Map<String, Integer> ages = Map.of("Al", 30);
for (var e : ages.entrySet()) {
    System.out.println(e.getKey());
}
🔮 Predict it

Your turn

The object really is an ArrayList. What happens?

Collection<String> c = new ArrayList<>();
c.add("a");
c.add("b");
System.out.println(c.get(1));
  1. b
  2. Compile error
  3. Throws IndexOutOfBoundsException
Show the answer

Compile error. The compiler only lets you call methods of the declared type. Collection has no get(int): index access belongs to List. Declare it as List<String> to get get.

⚠️ The trap

Looping over a Map directly

for-each needs an Iterable. A Map isn't one, so for (var x : map) doesn't compile. Pick the view you need: keys, values, or both via entrySet().

Map<String, Integer> m = new HashMap<>();
for (var x : m) { }          // ✗ won't compile
for (var e : m.entrySet()) { } // ✓
🤔 Think first

Spot the outsider

ArrayList, TreeSet, PriorityQueue, TreeMap. Which one is NOT a Collection?

Think about it, then reveal the answer

**TreeMap**. It implements Map (via SortedMap/NavigableMap), the separate branch. The others are a List, a Set and a Queue.

💼 In the real world

Program to the interface

Pros write List<Order> orders = new ArrayList<>(); and take Collection<Order> as a method parameter. The interface says what you need; the class is a swappable detail. Need sorting later? Swap the class, not every method signature. Interviewers love asking why.

Key takeaways

  1. Iterable → Collection → List / Set / Queue (Deque extends Queue)
  2. List: ordered, indexed, duplicates allowed
  3. Set: no duplicates; Queue: elements wait to be processed
  4. Map is NOT a Collection — iterate its keySet(), values() or entrySet()

💡 Think of a family tree: List, Set and Queue are siblings under Collection, while Map is a cousin from another branch.

🤯 Did you know?

The Collections Framework arrived in Java 1.2 (1998), designed mainly by Joshua Bloch. Before it, Java had only Vector, Stack and Hashtable.

Practice questions

Which one is NOT a subtype of Collection?

  1. ArrayList
  2. TreeSet
  3. PriorityQueue
  4. TreeMap
Check your answer

TreeMap. TreeMap implements Map (via SortedMap/NavigableMap), which is outside the Collection hierarchy. The other three are a List, a Set and a Queue.

Which of these can NOT be used directly in a for-each loop like for (var x : obj)?

  1. ArrayList<String>
  2. HashSet<String>
  3. HashMap<String, Integer>
  4. ArrayDeque<String>
Check your answer

HashMap<String, Integer>. for-each needs an Iterable. Map doesn't extend Iterable, so you loop over map.entrySet(), keySet() or values() instead.

Next: ArrayList is the list everyone uses. But what happens inside when it runs out of room?