Memra

The Collections framework map

◈ 5 cards

Collection vs Map, the List / Set / Queue interfaces and their implementations, and programming to the interface.

Two root hierarchies

The Java Collections Framework splits into two unrelated roots:

  • Collection<E> — a group of individual elements. Its three sub-interfaces are:
  • - List<E>ordered, allows duplicates, index access. Impls: ArrayList, LinkedList.
  • - Set<E>no duplicates. Impls: HashSet (no order), LinkedHashSet (insertion order), TreeSet (sorted).
  • - Queue<E> — ordering for processing (FIFO/priority). Impls: LinkedList, ArrayDeque, PriorityQueue.
  • Map<K,V> — key→value associations. Map is NOT a Collection — it has its own hierarchy. Impls: HashMap, LinkedHashMap, TreeMap.

Knowing this map lets you answer the exam's favourite question — "which collection would you use for X?" — by elimination: need key lookup → Map; need uniqueness → Set; need order/index/duplicates → List; need FIFO or priority processing → Queue.

### Program to the interface

Declare variables with the interface type and instantiate the concrete class only at new:

List<Event> events = new ArrayList<>();   // good: caller depends on List

This is exactly what the Greenhouse project's Controller does (private List<Event> eventList = new ArrayList<Event>();). The benefit: every method that takes or returns a List<Event> is decoupled from the implementation — you can swap ArrayList for LinkedList by changing one line, and no caller breaks. Declaring ArrayList<Event> events = ... instead would leak the implementation into every signature.

Worked example. A method that accepts any collection. Because it takes Collection<Event>, you can pass an ArrayList, a HashSet, a LinkedList, or anything else that implements Collection:

static int countReady(Collection<Event> events) {
    int n = 0;
    for (Event e : events)        // for-each works on any Iterable
        if (e.ready()) n++;
    return n;
}

The <> on the right (the diamond operator) infers the type argument from the left-hand declaration, so you write new ArrayList<>() not new ArrayList<Event>().

Collection<E>extends IterableList<E>ordered, duplicatesArrayListLinkedListSet<E>no duplicatesHashSetTreeSetQueue<E>FIFO or priorityArrayDequePriorityQueue
Read the middle row to answer "which collection for X?": the interface you pick is decided by the property you need, and only then do you name an implementation. Everything drawn here is a <code>Collection</code>, so everything here is <code>Iterable</code> and works with a for-each loop — and <code>Map</code> is nowhere on this tree.
Map<K,V>NOT a CollectionHashMapno orderLinkedHashMapinsertion orderTreeMapsorted keys
A second, unrelated root — <code>Map</code> extends nothing from the previous figure. That is what makes <code>for (Event e : someMap)</code> a compile error: a map is not <code>Iterable</code>, so you iterate one of its views instead, <code>entrySet()</code>, <code>keySet()</code> or <code>values()</code>.
NORMAL ~/memra/learn/comp-308/collections-framework-map utf-8 LF