
If you need a list in Java, use the ArrayList. It is faster than the LinkedList in almost all operations, and it needs about half as much memory per element.
At first glance, this contradicts the time complexity. According to it, the LinkedList should be ahead when inserting and removing: at the beginning of the list and at the position of an iterator, it needs O(1) for that, whereas the ArrayList needs O(n). I measured both JDK classes with JMH. The LinkedList is faster only at the beginning of the list and when inserting via a ListIterator. When removing, the ArrayList is just as fast with removeIf(); in all other measured operations, it is faster.
As a queue that is continuously appended to at the back and taken from at the front, an ArrayDeque is faster than the LinkedList.
In this article, you will learn:
- how
ArrayListandLinkedListdiffer, - how the two classes store their elements,
- what time complexity their operations have – and what the time complexity doesn’t tell you,
- how fast appending, iterating, access by index, inserting, and removing really are,
- how much memory an element costs in each of the two lists,
- in which cases a
LinkedListis worth it.
ArrayList vs. LinkedList: The Differences at a Glance
The following table compares the two classes. The time complexity applies to a list with n elements, the memory per element to a list of one million Integer objects:
ArrayList | LinkedList | |
|---|---|---|
| Data structure | array | doubly linked nodes |
| Interfaces | List, RandomAccess | List, Deque |
| Access by index | O(1) | O(n) |
| Appending at the end | O(1) amortized | O(1) |
| Inserting at the beginning | O(n) | O(1) |
| Inserting in the middle by index | O(n) | O(n) |
| Memory per element | 20.9 bytes | 40.0 bytes |
null and duplicates | allowed | allowed |
| Thread safety | no | no |
Both classes also implement Cloneable and Serializable.
The derivation of the time complexity is in the section Time Complexity: ArrayList vs. LinkedList. The byte counts are explained in the section Memory Usage: ArrayList vs. LinkedList.
How the Two Classes Store Their Elements
An ArrayList stores its elements in an array. The array is usually longer than the list – the free slots at the end are the reserve for the next calls to add(). When it is full, the ArrayList replaces it with an array that is 50% larger. How this works in detail is shown in the section How the ArrayList Works Internally of the article on the ArrayList.
A LinkedList creates a separate object for each element, a node. Each node has three fields: item refers to the element, next to the next node, and prev to the previous one. The LinkedList itself keeps the first node in first and the last node in last.
The following diagram shows the same four entries (first names of computer science pioneers) in both lists – at the top in an ArrayList with an array of six slots, at the bottom in a LinkedList with four nodes. The arrows between the nodes are the next references, the arrows below the nodes are the prev references:
All the differences I describe in the following sections follow from this structure. The theory behind it – arrays and linked lists in general, without reference to the Java classes – is in the article Array vs. Linked List.
Time Complexity: ArrayList vs. LinkedList
The following table shows the time complexity of the most important operations of both classes for a list with n elements:
| Operation | ArrayList | LinkedList |
|---|---|---|
get(i), set(i, e) | O(1) | O(n) |
add(e) at the end | O(1) amortized | O(1) |
addFirst(e), removeFirst() at the beginning | O(n) | O(1) |
add(i, e), remove(i) in the middle | O(n) | O(n) |
Iterator.remove(), ListIterator.add() | O(n) | O(1) |
removeIf(p) | O(n) | O(n) |
contains(o), indexOf(o) | O(n) | O(n) |
size() | O(1) | O(1) |
What O(1) and O(n) mean is explained in the article on Big O notation.
The O(1) of the LinkedList when inserting applies only where a reference to the node at the insertion point already exists: at the beginning (first), at the end (last), or at the position of an iterator that remembers its current node. For add(i, e), the LinkedList first has to find the node at position i. The private method node(int index) follows the references from node to node for this: starting at first if the index is in the front half, otherwise starting at last. That is up to n / 2 steps, i.e., O(n).
What the table doesn’t show: O(n) is not the same as O(n). When the ArrayList inserts in the middle, it shifts the back half of its array with System.arraycopy(). This copies a contiguous memory area in large blocks. The LinkedList, on the other hand, follows n / 2 references, and each step depends on the previous one: only once a node has been loaded from memory does the CPU know the address of the next node. The measurement in the section Inserting in the Middle shows how big this difference is.
Benchmarks: Appending, Iterating, and Access by Index
For each operation, I measured with JMH how long it takes on both classes, with Integer elements and three or four list sizes. The benchmarks are in the GitHub repository java-collections-guide, in the package eu.happycoders.collections.arraylistvslinkedlist.benchmark. The machine and settings are listed in the info box at the end of this section.
Appending at the End
The benchmark AppendBenchmark appends n elements to an empty list – to an ArrayList with default capacity, to one with the initial capacity n, and to a LinkedList. The table shows how long this takes for all n elements together. For the LinkedList with ten million elements, it shows a range instead of a mean – the reason is below the table:
| Elements | ArrayList | ArrayListwith initial capacity | LinkedList |
|---|---|---|---|
| 1,000 | 1.4 µs | 1.1 µs | 2.6 µs |
| 100,000 | 115 µs | 104 µs | 260 µs |
| 1,000,000 | 1.6 ms | 1.1 ms | 2.7 ms |
| 10,000,000 | 18 ms | 11 ms | 35 to 660 ms |
Up to one million elements, the LinkedList takes 1.7 to 2.3 times as long as the ArrayList with default capacity. It creates a 24-byte node for each element, while the ArrayList only writes a reference into its array – and copies it into a larger one from time to time.
With the initial capacity n, the ArrayList can avoid these copies: then it needs almost a third less time for one million elements (1.1 instead of 1.6 ms).
With ten million elements, the time of the LinkedList varies between 35 and 660 ms depending on the JVM start and the measurement. This is where the garbage collector comes into play: the ten million nodes occupy 240 MB, and they all remain referenced until the end of the operation. Every young collection during filling therefore has to follow the references from node to node and copy every node of the young generation that it finds along the way.
In a follow-up measurement with the JMH GC profiler, the garbage collector ran for a total of 22 seconds during the 20 measurements with the LinkedList, but only 2.7 seconds during the 20 measurements with the ArrayList with initial capacity.
Iterating
The benchmark IterateBenchmark sums all elements with a for-each loop:
| Elements | ArrayList | LinkedList |
|---|---|---|
| 1,000 | 0.32 µs | 1.5 µs |
| 100,000 | 38 µs | 152 µs |
| 10,000,000 | 3.7 ms | 15.4 ms |
The LinkedList takes four to five times as long as the ArrayList. Both lists load the Integer object for each element. The LinkedList additionally loads the node, and only from the node does it learn the address of the next node. The ArrayList, on the other hand, reads its references one after another from a contiguous array – so with each cache line, the CPU loads several references at once. More on this in the section Locality of the article “Array vs. Linked List”.
This measurement shows the most favorable case for the LinkedList: all elements were appended one after another at the end, so the nodes lie in memory in the order in which they were created – i.e., in list order. What happens when that is not the case is shown by the benchmark ScatteredIterateBenchmark. It builds a second LinkedList with add(index, e) at random positions, like a list into which a program has inserted at arbitrary positions for a long time. Neighbors in the list then no longer lie next to each other in memory.
With 100,000 elements, the for-each loop over the ordered LinkedList takes 151 µs, over the scattered one 781 µs – five times as long. That is about 20 times as long as the ArrayList.
However, this factor of 20 doesn’t survive the young collections. A LinkedList that lives longer is copied by the garbage collector, and afterward its nodes lie differently. The benchmark ScatteredAfterGcBenchmark therefore measures the same lists again after the garbage collector has run:
| State of the lists | ordered | scattered |
|---|---|---|
| freshly built | 151 µs | 781 µs |
| after 20 young collections | 180 µs | 176 µs |
after System.gc() | 151 µs | 784 µs |
After the young collections, the scattered list is as fast as the ordered one. A young collection copies every live node that it reaches via a reference to the next free address in the target region and then processes the references of the copy. In this way, it follows the chain from node to node and places the nodes one after another in the target region – no matter how scattered they were before.
That both lists are then slightly slower than the freshly built ordered list (176 and 180 instead of 151 µs) is due to the division of labor: several GC threads copy in parallel, each writing into its own memory area. With only one GC thread (-XX:ParallelGCThreads=1), both lists come in at 153 to 155 µs after the young collections, i.e., roughly on par with the freshly built ordered list.
A full GC, triggered by System.gc(), on the other hand, only slides the live objects together: it goes through memory region by region from front to back and gives each object the next free address. The order of the objects is preserved, and the scattered list stays scattered.
I measured with G1, the default garbage collector. With a different collector, the result may differ.
For a list that lives in your program for a while, the factor of four to five is therefore realistic.
Access by Index
The benchmark RandomAccessBenchmark reads an element at a random position with get(i). The table shows the time for one call:
| Elements | ArrayList | LinkedList |
|---|---|---|
| 1,000 | 1.0 ns | 285 ns |
| 10,000 | 1.1 ns | 3,488 ns |
| 100,000 | 1.3 ns | 37,922 ns |
| 1,000,000 | 1.5 ns | 388,561 ns |
The ArrayList needs 1.0 to 1.5 ns – it reads the element directly from its array. The time rises by half with the size because the accesses are spread over more memory: on each call, the JVM reads the reference in the array and, to check the type of the element, the header of the Integer object. With 1,000 elements, the array and the Integer objects, at 20 KB together, fit into the CPU’s L1 data cache; from 10,000 elements (200 KB) on, they no longer do.
The LinkedList starts the search at the beginning or at the end of the list, depending on the index – i.e., in the half in which the index lies – and on average traverses half of that half. That is n / 4 references, so 250,000 for one million elements. This takes 389 µs, about 250,000 times as long as the ArrayList.
Access by index on a LinkedList becomes even more expensive when a loop runs over all indices and calls get() for each index. The benchmark IndexLoopBenchmark measures the following loop:
for (int i = 0; i < list.size(); i++) {
sum += list.get(i);
}
With 100,000 elements, it takes 30 µs on the ArrayList and 3.7 seconds on the LinkedList – about 120,000 times as long. Each get(i) starts again from the nearer end, so the loop over the LinkedList costs O(n²): with ten times as many elements, it takes a good 100 times as long.
So: if you don’t know which List implementation is behind a List variable, iterate with for-each. The loop over the index is only suitable for lists that implement the marker interface RandomAccess, such as the ArrayList – with it, both loops are about equally fast.
Some methods of the JDK class Collections use instanceof RandomAccess to check whether the list implements the interface and choose their algorithm accordingly:
Collections.binarySearch(): searches aRandomAccesslist and lists with fewer than 5,000 elements withget(i). In all other lists, it moves aListIteratorto the next middle each time.Collections.shuffle(): swaps the elements in place in aRandomAccesslist and in lists with fewer than five elements. It first copies all other lists into an array, shuffles the array, and writes the elements back into the list with aListIterator.
Benchmarks: Inserting and Removing
The following benchmarks insert elements into the list or remove elements from it. The machine and settings are the same as in the previous section.
Inserting and Removing at the Beginning
The benchmark FrontBenchmark inserts n elements at the beginning of an empty list and then takes them all out again at the beginning. Both lists use addFirst(e) and removeFirst() for this, which every List has had since Java 21 – introduced with the Sequenced Collections. In the ArrayList, they do the same as add(0, e) and remove(0). As a third candidate, I measured the ArrayDeque, with the same methods:
| Elements | ArrayList | LinkedList | ArrayDeque |
|---|---|---|---|
| 1,000 | 51 µs | 2.9 µs | 2.8 µs |
| 10,000 | 4.6 ms | 26 µs | 31 µs |
| 100,000 | 597 ms | 229 µs | 267 µs |
The table shows the total time for all n elements. Per element – inserting once, removing once – LinkedList and ArrayDeque need 2 to 3 ns in all three sizes, so their total time grows linearly with n. With the ArrayList, even the time per element grows with the length of the list, from 51 ns to 6 µs.
This is the case the LinkedList is built for. The ArrayList shifts all elements on every call and needs 597 ms for 100,000 elements – the LinkedList merely links a node to first or unlinks it and needs 229 µs. The ArrayList thus takes about 2,600 times as long.
Against the ArrayDeque, the LinkedList wins narrowly in this benchmark from 10,000 elements on: the ArrayDeque is 17 to 21% slower. With 1,000 elements, the two are on par.
The benchmark fills the list once and then empties it again – more the way a stack is used. In many programs, however, a queue works differently: it holds a certain number of elements for a while, and the program appends as many elements at the back as it takes out at the front. This steady state is measured by the benchmark QueueBenchmark: each operation appends an element with addLast(e) and takes one from the beginning with removeFirst(). The table shows the time for this pair:
| Elements in the queue | LinkedList | ArrayDeque |
|---|---|---|
| 100 | 3.7 ns | 3.3 ns |
| 10,000 | 3.7 ns | 2.3 ns |
| 1,000,000 | 3.9 ns | 2.4 ns |
In the steady state, the ArrayDeque wins in all three sizes; with one million elements, the LinkedList is 66% slower. The ArrayDeque writes its elements into a ring buffer whose slots it reuses, and according to the GC profiler, it creates no object per operation. The LinkedList creates a 24-byte node for each appended element.
Inserting in the Middle
The benchmark MiddleInsertBenchmark inserts n elements with add(list.size() / 2, e), each in the middle of an initially empty list:
| Elements | ArrayList | LinkedList |
|---|---|---|
| 1,000 | 15 µs | 278 µs |
| 10,000 | 1.2 ms | 35 ms |
| 100,000 | 132 ms | 3.6 s |
The LinkedList takes 19 to 29 times as long. Both classes perform n / 2 steps per call: the ArrayList shifts the back half of its array, the LinkedList follows the references from one end to the middle. Why shifting is so much faster than following the references is shown in the section Time Complexity: ArrayList vs. LinkedList.
Inserting and Removing While Iterating
If an iterator is already at the right position, the LinkedList actually inserts at any position in O(1) and removes in O(1). The benchmark IteratorModificationBenchmark measures three operations with 100,000 elements:
Iterator.remove(): removes every element with an odd value while iterating, i.e., every second one.removeIf(): removes the same elements withlist.removeIf(value -> value % 2 != 0).ListIterator.add(): inserts a new element after each element while iterating.
Because each of these operations changes the list, each run works on a fresh copy of the same initial list. I measured the time for copying separately and subtracted it in the table:
| Operation | ArrayList | LinkedList |
|---|---|---|
Iterator.remove() | 151 ms | 166 µs |
removeIf() | 153 µs | 165 µs |
ListIterator.add() | 291 ms | 321 µs |
With Iterator.remove() and ListIterator.add(), the LinkedList is about 900 times as fast as the ArrayList. The ArrayList shifts all elements behind the iterator on every call; the LinkedList only relinks references.
If the ArrayList removes the elements with removeIf() instead, it is as fast as the LinkedList. ArrayList.removeIf() marks the elements to be removed in a first pass and compacts the remaining ones in a second – each element at most once. The LinkedList has no removeIf() method of its own; it inherits the default method from Collection, which works with Iterator.remove(). That is why it takes the same time with both variants.
For inserting at many different positions, the ArrayList has no such counterpart – addAll(index, c) inserts several elements only at one position. Here, the LinkedList keeps its lead.
Memory Usage: ArrayList vs. LinkedList
A LinkedList with one million Integer elements occupies 40.0 bytes per element, an ArrayList 20.9 bytes. In both lists, 16 bytes of this are for the Integer object.
The remaining bytes are made up as follows:
LinkedList: one 24-byte node per element – the object header and the three referencesitem,next, andprevwith 4 bytes each. With Compact Object Headers, the default since Java 27, the header is 8 bytes, so the node is 20 bytes, rounded up to 24. Without Compact Object Headers, the header is 12 bytes, and the node is exactly 24 bytes.ArrayList: a 4-byte reference in the array plus the reserve at the end of the array. For the one million elements, the array has 1,215,487 slots and occupies 4,861,960 bytes, i.e., 4.9 bytes per element.
The following diagram shows these bytes for one element. The widths are to scale: a reference (4 bytes) is half as wide as an object header (8 bytes). Both lists refer to an Integer object of the same size, here with the value 1000:
Without the Integer objects, the LinkedList thus needs almost five times as much memory as the ArrayList: 24 versus 4.9 bytes per element. The 4 bytes per reference apply with compressed oops, i.e., for heaps up to 32 GB. How memory usage is calculated in general is shown in the section Memory Consumption of the article “Array vs. Linked List”.
When Is a LinkedList Worth It?
According to the measurements, there are two cases in which the LinkedList is the better choice:
- Many insertions via a
ListIteratorin a long list: If you insert new elements at many positions while iterating, theLinkedListneeds O(1) for each, theArrayListO(n). With 100,000 elements, that was 321 µs versus 291 ms – a factor of just over 900. This doesn’t apply to removing – for that, theArrayListhasremoveIf(). - List and deque in one object: The
LinkedListimplementsListandDeque. If you need both for the same data, e.g., a queue that you occasionally read by index, theLinkedListcovers both. With theArrayList, taking elements from the beginning is more expensive – O(n); theArrayDequehas no access by index.
In all other cases, the ArrayList is faster or equally fast and needs less memory.
Queue: ArrayDeque Instead of LinkedList
If you need a queue, the ArrayDeque is the better choice. As a queue that is continuously appended to at the back and taken from at the front, the LinkedList was up to 66% slower, and the ArrayDeque creates no object when inserting. How it works internally and how it differs from the LinkedList is shown in the section ArrayDeque vs. LinkedList of the article on the ArrayDeque. The recommended use as a deque in the article on the LinkedList comes to the same conclusion.
Summary
An ArrayList stores its elements in an array, a LinkedList in one node per element. In terms of time complexity, the LinkedList wins at the beginning of the list and when inserting and removing via an iterator, the ArrayList for access by index.
In the benchmarks, the ArrayList wins almost everywhere. It appends at the end 1.7 to 2.3 times as fast and iterates four to five times as fast. In the middle, the ArrayList inserts 19 to 29 times as fast, although both classes need O(n) for this – shifting an array is faster than following references. Per element, the ArrayList needs 20.9 instead of 40.0 bytes.
The LinkedList wins only at the beginning of the list and when inserting via a ListIterator. As a queue that is continuously appended to at the back and taken from at the front, however, the ArrayDeque is faster than the LinkedList.
My recommendations:
- Use the
ArrayListas the default for lists. - Iterate over lists with for-each, not by index – an index loop over a
LinkedListcosts O(n²). - Remove elements by a condition with
removeIf(), also from anArrayList. - Use an
ArrayDequefor queues. - Use a
LinkedListonly if you insert at many positions while iterating over a long list or need the same collection as aListand as aDeque.
What should I cover next? The best way for me to find out is through your feedback: a review on my ProvenExpert profile shows me which topics matter to you – and motivates me to write more articles.




