The collections hierarchy in Java
Iterable → Collection → List / Set / Queue; Map is separate.
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<>(); // lineMap: 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());
}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));bCompile errorThrows 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.
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()) { } // ✓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.
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
- Iterable → Collection → List / Set / Queue (Deque extends Queue)
- List: ordered, indexed, duplicates allowed
- Set: no duplicates; Queue: elements wait to be processed
- 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.
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?
- ArrayList
- TreeSet
- PriorityQueue
- 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)?
- ArrayList<String>
- HashSet<String>
- HashMap<String, Integer>
- 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.