Memra

Framework overview: Collection vs Map

The core interfaces — List, Set, Queue/Deque, Map — and how Iterable fits in.

Two hierarchies, not one

The Java Collections Framework is split into two parallel hierarchies:

- Collection<E> — for groups of individual elements. Sub-interfaces: List, Set, Queue, Deque. - Map<K,V> — for key→value mappings. Not a Collection.

Iterable<T>
  └── Collection<E>
        ├── List<E>       — ordered, indexed, duplicates allowed
        ├── Set<E>        — no duplicates
        └── Queue<E>      — FIFO/priority access
              └── Deque<E> — double-ended queue (stack or queue)

Map<K,V>                  — keys unique, values may repeat
  ├── HashMap
  ├── LinkedHashMap
  └── SortedMap → TreeMap

Iterable<T> sits above Collection in the hierarchy. Any class that implements Iterable can be used in a for-each loop. It has one method: Iterator<T> iterator(). Iterator itself exposes hasNext() and next().

The most important concrete classes paired with their interfaces:

| Interface | Common implementations | |---|---| | List | ArrayList, LinkedList | | Set | HashSet, LinkedHashSet, TreeSet | | Queue/Deque | ArrayDeque, LinkedList | | Map | HashMap, LinkedHashMap, TreeMap |

Programming to the interface (List<String> list = new ArrayList<>()) is the recommended idiom: it lets you swap implementations without changing any other code.

NORMAL ~/memra/learn/java-from-zero/collections-framework-overview utf-8 LF