Skip to content

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

    Collections, From the Inside

    ArrayList, LinkedList, HashMap, TreeMap, heaps and queues — how each works inside and what every operation costs.

    • Module 7 of 11
    • Intermediate
    • 11 chapters
    • 1 h 2 min

    Collections are the toolbox for every DSA problem. Knowing how each works inside tells you which to pick and what every operation really costs.

    What you will be able to do

    • Explain how ArrayList, LinkedList and HashMap work inside
    • Use TreeMap, PriorityQueue and ArrayDeque for the right jobs
    • Define ordering with Comparable and Comparator
    • Pick the right collection for a problem

    Chapters

    1. The Map of the FrameworkInterfaces (03javaoop.md Ch 6) describe the job: List, Set, Queue, Deque, Map.4 min2. Lists: ArrayList and LinkedListArrayList stores elements in an internal array (01javabasics.md Ch 8). Default starting capacity 10; when full, it grows by 1.5× and5 min3. Iterating SafelyAn Iterator walks through a collection: hasNext(), next(), and remove() (removes the last element returned, safely). The for-each loop2 min4. HashMap, Inside OutA HashMap<K, V> stores key → value pairs. Look-up, insertion and removal by key are O(1) on average.11 min5. HashSet, LinkedHashMap and an LRU CacheHashSet<E> is a set of unique items. Inside, it's literally a HashMap<E, Object> where each item is a key and every value is the same4 min6. TreeMap and TreeSet: Always SortedTreeMap<K, V> keeps its keys sorted at all times. TreeSet<E> is a sorted set (it's a TreeMap inside, like HashSet/HashMap).4 min7. Comparable and Comparator: Defining "Order"Comparable<T> — one method, int compareTo(T other), implemented by the class itself. String, Integer and records you write can3 min8. Queues, Stacks and ArrayDequeFIFO (first in, first out) — queue. LIFO (last in, first out) = stack.4 min9. PriorityQueue, Inside OutA PriorityQueue<E> always gives you the smallest element first (by natural order or a comparator) — a min-heap by default.7 min10. Utilities and Immutable CollectionsList<Integer> l — new ArrayList<>(List.of(3, 1, 2));1 min11. Choosing the Right CollectionTaught step by step, with real output.2 min

    Extras