Lesson · Collections Framework
List: ArrayList vs LinkedList, settled
14 min readList
The List contract (ordered, indexed, duplicates allowed) and the two implementations everyone compares. The punchline: the complexity table says trade-off, reality says ArrayList almost every time.
The contract
A List is a sequence: elements keep their position, the same value may appear many times, and every element is reachable by index. get(i), set(i, e), add(i, e), remove(i), indexOf, subList, sort: that surface is the list.
You declare List<T> and instantiate once: List<String> names = new ArrayList<>();. Everything downstream sees the contract, so the implementation stays a one-line decision.
ArrayList: a growable array
ArrayList is an array that pretends to be unbounded. Two numbers matter: size (elements you've added) and capacity (slots actually allocated). When the array fills, it grows by roughly 1.5×, copying every element into the bigger array.
Because growing is rare and copying is a fast block move, appending averages out to O(1), amortized constant. Random access is always O(1): index arithmetic and one bounds check. The price is paid in the middle: inserting or removing shifts everything after the position, O(n).
One practical consequence: because elements sit contiguously, iteration is cache-friendly. The CPU prefetches the next element before you ask for it.
Same interface · two layouts
ArrayList · capacity 4
LinkedList · prev ⇄ next nodes
Same data, two layouts. Left: a contiguous array with spare capacity. Right: a doubly-linked chain of nodes. Watch identical operations land differently.
LinkedList: a chain of nodes
LinkedList stores each element in a node holding the item plus references to the previous and next node. Inserting or removing is just relinking: genuinely O(1), once you are standing at the position, which means holding an iterator there.
Finding that position is the catch: get(i) must walk the chain from one end, O(n), and it walks node by node through scattered heap memory, missing the cache at every hop. Each element also pays node overhead: two references plus a header, roughly three times the array slot for a small value.
LinkedList is the only List that also implements Deque, but even that is no longer its selling point: ArrayDeque does the job better.
Chain anatomy · where the links live
pick an index and watch both layouts answerArrayList · one contiguous block
Slot 2 lives at base + 2 × cell size: get(2) is address arithmetic, one step, O(1).
LinkedList · nodes scattered in memory
get(2) starts at the head and follows 2 next links: the walk is the cost, O(n).
Each node also stores a link back (previous), so movement near the ends goes both ways. The list itself holds only the two ends.
Same operations, different bills
The stepper above runs both structures through the same operations; this diagram is the anatomy behind the costs.
Choosing, and the sharp edges
Default to ArrayList. Choose LinkedList only if you genuinely splice through a held iterator constantly at large sizes; choose ArrayDeque for queue/stack work; choose CopyOnWriteArrayList or a synchronized wrapper when the list crosses threads.
Know the size up front? Pre-size it: new ArrayList<>(expectedSize) skips the intermediate resizes.
Four edges worth memorising: remove(1) removes the element at index 1 while remove(Integer.valueOf(1)) removes the value 1: overload resolution, not erasure. subList returns a view, not a copy: clearing it deletes from the parent. Arrays.asList is fixed-size, and List.of is immutable. And an ArrayList shared between threads without synchronisation can corrupt or throw: that is the race conditions lesson wearing a collection costume.
remove(1) is not remove the value 1
With a List<Integer>, list.remove(1) binds to remove(int index) and deletes the second element; list.remove(Integer.valueOf(1)) binds to remove(Object) and deletes the value 1. Autoboxing chose the overload for you, and it chose the index one.
Interview angles
ArrayList vs LinkedList: what does the complexity table say, and what does reality say?+
The table: ArrayList O(1) random access and O(n) insert/remove in the middle; LinkedList the mirror image. Reality: LinkedList only wins when you hold the iterator at the splice point; for everything else the array's memory locality beats pointer chasing, so ArrayList wins even at moderate mid-insert sizes.
Why is appending to an ArrayList O(1) if it sometimes copies everything?+
Amortized analysis: growth is geometric (~1.5×), so a copy of n elements is paid for by the n appends that filled the array, so the total work over N appends stays O(N). Individual appends are O(1) except the rare resize.
What does subList actually return?+
A view backed by the original list, not a copy. Writes through it write through to the parent, clearing it deletes that range from the parent, and structurally modifying the parent behind the view's back throws ConcurrentModificationException.
When do you reach past both implementations?+
Need a queue or stack? ArrayDeque. Need thread-safety? CopyOnWriteArrayList (read-heavy) or a concurrent structure. Need a fixed, immutable list? List.of / an array. LinkedList stays for one niche: constant splicing through a held iterator.