Skip to content

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

    Java / Collections, From the Inside

    Lists: ArrayList and LinkedList

    ArrayList stores elements in an internal array (01javabasics.md Ch 8). Default starting capacity 10; when full, it grows by 1.5× and

    Chapter 2 of 115 min read

    The Plain Picture#

    • ArrayList — a row of lockers that can move to a bigger row when full. Reaching locker #500 is instant. Inserting at the front means everyone shifts one locker along.

    • LinkedList — a treasure hunt: each clue says where the next one is. Adding a clue in the middle is easy once you're there, but getting to clue #500 means following 500 clues.

    The Real Names#

    • ArrayList stores elements in an internal array (Java Basics Ch 8). Default starting capacity 10; when full, it grows by 1.5× and copies — amortized O(1) add (Big-O and Complexity Ch 7).

    • LinkedList is a doubly linked list: each element sits in a separate node object holding the value and references to the previous and next nodes. (Linked lists as a data structure are taught in Linked Lists (coming soon).)

      ArrayList                                    LinkedList
      ┌─────┬─────┬─────┬─────┬─────┬─────┐       ┌───────┐    ┌───────┐    ┌───────┐
      │Intro│Cells│Gene…│     │     │     │       │ Intro │ ⇄  │ Cells │ ⇄  │ Gene… │
      └─────┴─────┴─────┴─────┴─────┴─────┘       └───────┘    └───────┘    └───────┘
       size = 3, capacity = 6                      each node: value + prev + next references
       one contiguous block → cache-friendly       scattered objects → cache misses

    Under the Hood#

    The List API#

    Java
    List<String> notes = new ArrayList<>();
    
    notes.add("Cells"); // add at the end
    
    notes.add("DNA");
    
    notes.add(0, "Intro"); // insert at index 0 (shifts others right — O(n))
    
    notes.set(2, "Genetics"); // replace index 2
    
    System.out.println(
        notes +
            " size=" +
            notes.size() +
            " get(1)=" +
            notes.get(1) +
            " indexOf=" +
            notes.indexOf("Genetics") +
            " contains=" +
            notes.contains("DNA")
    );
    // [Intro, Cells, Genetics] size=3 get(1)=Cells indexOf=2 contains=false

    Also: isEmpty(), remove(int index), remove(Object o), clear(), subList(from, to), sort(comparator), addAll(other).

    The remove overload trap (from Classes, Objects and Memory Ch 8):

    Java
    List<Integer> nums = new ArrayList<>(List.of(10, 20, 30, 1));
    
    nums.remove(1); // removes the element AT INDEX 1 (20)   → [10, 30, 1]
    
    nums.remove(Integer.valueOf(1)); // removes the VALUE 1                   → [10, 30]

    ArrayList vs LinkedList — the classic comparison#

    OperationArrayListLinkedList
    get(i) / set(i, x)O(1)O(n) — walk from an end
    add/remove at the endamortized O(1)O(1)
    add/remove at the frontO(n) — shift everythingO(1)
    add/remove in the middleO(n) shiftO(n) to walk there, then O(1)
    memory per element~4–8 bytes (a reference)a whole node object (~24+ bytes) + the reference
    cache behaviourexcellent (contiguous)poor (scattered nodes — How Computers Work Ch 6)

    In practice, use ArrayList. Even in its "good" cases, LinkedList's node allocation and cache misses usually make it slower, and for adding and removing at both ends, ArrayDeque (Ch 8) beats it. LinkedList is almost never the right answer in modern Java — and saying so, with the reasons above, is a strong interview answer.

    Fixed and immutable lists#

    Java
    List.of(1, 2).add(3); // UnsupportedOperationException — List.of is fully IMMUTABLE
    
    List<Integer> fixed = Arrays.asList(1, 2, 3);
    
    fixed.set(0, 9); // allowed → [9, 2, 3]
    
    fixed.add(4); // UnsupportedOperationException — Arrays.asList is FIXED-SIZE (backed by the array)
    
    new ArrayList<>(List.of(1, 2)); // a normal, growable copy

    List.of also rejects null elements.

    Check yourself

    4 questions
    1

    Why is ArrayList.get(i) O(1) but LinkedList.get(i) O(n)?

    Show answer

    ArrayList computes the address directly from the index; LinkedList must follow node links one by one.

    2

    Why is LinkedList usually slower than ArrayList even for operations with the same Big-O?

    Show answer

    Each element is a separate object: more memory, more allocation, and scattered nodes cause cache misses.

    3

    What does list.remove(1) do on a List<Integer>?

    Show answer

    Removes the element at index 1 (use remove(Integer.valueOf(1)) to remove the value 1).

    4

    What's the difference between List.of, Arrays.asList and new ArrayList<>(...)?

    Show answer

    List.of is immutable; Arrays.asList is fixed-size but settable (backed by an array); new ArrayList<> is fully mutable and growable.