Start With the Problem#
StudyBuddy needs to hold data in six very different ways:
a student's notes in order, where new notes are added at the end and notes are opened by position;
"find the student with this email" among 50,000 students — instantly;
the unique tags on all notes ("biology", "exam", …), with no duplicates;
a leaderboard always sorted by score, where you can ask "who's just above 170 points?";
a queue of AI jobs processed in arrival order, and an undo stack for edits;
"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| Interface | Job | Duplicates? | Order |
|---|---|---|---|
List | a sequence you access by position | yes | the order you put things in |
Set | a group of unique items | no | depends on the implementation |
Queue / Deque | take items out from the front (and/or back) | yes | FIFO / both ends |
Map | look up a value by its key | keys unique, values may repeat | depends 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 questionsInterface vs implementation — give one of each.
Show answerHide answer
Interface List, implementation ArrayList (or Map / HashMap, etc.).
Which interface would you use for: notes in order; unique tags; email → student; a job queue?
Show answerHide answer
List; Set; Map; Queue (or Deque).
Is Map a Collection?
Show answerHide answer
No — it's a separate hierarchy, because it holds pairs.