Collections
Lists, sets, maps and queues — how they work and which one to reach for.
Take this course in the learning hubLessons
- The Collection familyKnow the four shapes of data the JDK gives you and how they relate to each other.
- ArrayList: your default ListUse ArrayList confidently: add, get, set, remove, copy, and pre-size it.
- ArrayList vs LinkedListSee where each list wins, and why ArrayList is still the right default almost always.
- Sets: HashSet, LinkedHashSet, TreeSetStore unique values and pick the set whose ordering guarantee you actually need.
- equals and hashCodeUnderstand why hash collections silently misbehave when these two methods disagree.
- HashMap in daily useHandle missing keys, counters and grouping without a single null check.
- How HashMap works insideExplain buckets, resizing and the treeify threshold well enough to reason about performance.
- LinkedHashMap, TreeMap, EnumMapChoose the map that gives you insertion order, sorted keys, or enum-keyed speed.
- Queue, Deque and PriorityQueueModel stacks, queues and priority ordering with the right JDK type.
- Iteration and fail-fastRemove elements safely and understand why ConcurrentModificationException fires.
- Comparable, Comparator, sortingDefine one natural order and build any number of alternative orders on demand.
- Immutable collectionsCreate unmodifiable collections and tell a real copy apart from a read-only view.
- The Collections utility classUse the one-line helpers in java.util.Collections instead of hand-rolling them.
- Classic collection trapsRecognise the four bugs that catch almost every Java developer at least once.
- Concurrent collectionsShare collections between threads without locks and without lost updates.
- Choosing the right collectionAnswer four questions and land on the correct collection every time.
- Arrays vs collectionsKnow when a plain array is enough, and what a List buys you for its extra weight.
- Converting arrays and listsMove between arrays and lists in both directions without hitting the asList traps.
- Generics and wildcardsRead ? extends and ? super without guessing, using one rule you can say out loud.
- Iterator vs ListIteratorWalk a collection by hand when for-each is not enough, forwards or backwards.
- removeIf and bulk operationsDelete, keep and rewrite whole collections in one call instead of writing a loop.
- Map.merge and the compute familyUpdate an entry from whatever is already there, in one call, with no null checks.
- TreeMap navigation methodsAnswer nearest-key and range questions without scanning the whole map.
- PriorityQueue orderingAlways get the smallest element out first, and see why printing the heap looks wrong.
- EnumMap and EnumSetUse the array-backed collections that exist only for enum keys and enum elements.
- Nested collectionsBuild a map of lists, and know when a copy still shares the collections inside it.
- Sorting stability and safe comparatorsSee why two sorts in a row still work, and why comparing with a - b is a real bug.
- Capacity, load factor and resizingSize a HashMap or ArrayList up front and skip the repeated copy-and-rehash.
- Nulls in collectionsKnow exactly which collections accept null and which throw the moment they see one.
- Big-O cheat sheetThe real cost of the operations you repeat, for the collections you actually use.
- Sequenced collections (Java 21)Read and write both ends of any ordered collection with one API, new in Java 21.
- Views: keySet, values and subListTell a live view apart from a copy, and stop a subList blowing up underneath you.
- Vector, Hashtable and synchronizedMapUnderstand why locking every method is not thread safety, and what to use instead.
- WeakHashMap and IdentityHashMapTwo maps that change the rules: one forgets by itself, one ignores equals entirely.
- When two collections are equalCompare collections across implementations and know exactly when equals says false.
- Sorting a map by value, and top-NRank entries by value, and keep the largest few without sorting the whole thing.
- Collections in your APIPass collections across a boundary without leaking your state or returning null.
- Writing your own IterableMake your own type work in a for-each loop by implementing Iterator correctly.
- Comparison method violates its general contractA sort that works on small lists and throws on large ones.
- Mutable keys and entries that disappearChange a key after insertion and the value is unreachable but still occupying memory.
- What a collection actually costs in memoryBoxing and node overhead make a Map of Integers far larger than the numbers in it.
- Why an unsynchronised HashMap is not merely unsafeLost updates are the mild outcome; corruption and stale reads are the real risk.