Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Java / Collections, From the Inside

    The Map of the Framework

    Interfaces (03javaoop.md Ch 6) describe the job: List, Set, Queue, Deque, Map.

    Chapter 1 of 114 min read

    Start With the Problem#

    StudyBuddy needs to hold data in six very different ways:

    1. a student's notes in order, where new notes are added at the end and notes are opened by position;

    2. "find the student with this email" among 50,000 students — instantly;

    3. the unique tags on all notes ("biology", "exam", …), with no duplicates;

    4. a leaderboard always sorted by score, where you can ask "who's just above 170 points?";

    5. a queue of AI jobs processed in arrival order, and an undo stack for edits;

    6. "the 10 highest scores" out of millions, without sorting them all.

    An array does none of these well: it can't grow, finding an email means scanning all 50,000, it doesn't prevent duplicates, keeping it sorted means shifting elements on every insert. Java's Collections Framework provides a ready-made, battle-tested structure for each job. This file shows what each one is inside, because that's what decides what it costs.

    The Plain Picture#

    A kitchen has jobs — "somewhere to store dry food", "somewhere to keep things cold" — and products that do each job: a steel shelf or a wooden one; a single-door fridge or a double-door one. You plan the kitchen by the job, then pick a product.

    The Real Names#

    • Interfaces (Object-Oriented Programming Ch 6) describe the job: List, Set, Queue, Deque, Map.

    • Implementations are the products: ArrayList, HashSet, ArrayDeque, HashMap, TreeMap, …

    • Declare variables with the interface, build with the implementation — programming to an interface: List<String> notes = new ArrayList<>();

                                      Iterable
                                         │
                                     Collection
                 ┌───────────────────────┼─────────────────────────┐
                List                    Set                       Queue
                 │               ┌───────┴────────┐                 │
          ┌──────┴──────┐     HashSet          SortedSet         ┌──┴──────────┐
      ArrayList   LinkedList     │                │            Deque      PriorityQueue
                            LinkedHashSet      TreeSet           │
                                                           ┌─────┴─────┐
                                                       ArrayDeque  LinkedList
    
                                       Map   (separate — holds key → value pairs)
                         ┌──────────────┼───────────────┐
                      HashMap        SortedMap        EnumMap
                         │              │
                   LinkedHashMap     TreeMap
    InterfaceJobDuplicates?Order
    Lista sequence you access by positionyesthe order you put things in
    Seta group of unique itemsnodepends on the implementation
    Queue / Dequetake items out from the front (and/or back)yesFIFO / both ends
    Maplook up a value by its keykeys unique, values may repeatdepends on the implementation

    All collections hold objects, so primitives are autoboxed (Classes, Objects and Memory Ch 8): List<Integer>, never List<int> (Exceptions and Generics Ch 9).

    Check yourself

    3 questions
    1

    Interface vs implementation — give one of each.

    Show answer

    Interface List, implementation ArrayList (or Map / HashMap, etc.).

    2

    Which interface would you use for: notes in order; unique tags; email → student; a job queue?

    Show answer

    List; Set; Map; Queue (or Deque).

    3

    Is Map a Collection?

    Show answer

    No — it's a separate hierarchy, because it holds pairs.