ArrayList vs LinkedList in Java
8 min readUpdated
ArrayList and LinkedList both implement List, so the same code works with either. Inside they are completely different, and that changes how fast each operation is. Here is the difference, and why ArrayList is the right default almost every time.
The short answer
- Use ArrayList by default: fast access by index, fast iteration, less memory.
- Use ArrayDeque (not
LinkedList) when you need a queue or a stack. - Choose LinkedList only when you add and remove in the middle through a
ListIteratora lot, which is rare.
Comparison table
| ArrayList | LinkedList | |
|---|---|---|
| Inside | A resizable array | A doubly linked list of nodes |
get(i) / set(i, x) | O(1) | O(n): walks from the nearer end |
add(x) at the end | O(1) amortised | O(1) |
| Add or remove at the start | O(n): shifts every element | O(1) |
| Add or remove in the middle | O(n): shifts elements | O(n) to find the spot, O(1) to link |
| Memory per element | One reference in the array | A node with the value and two links |
| Iteration speed | Fast (elements next to each other in memory) | Slower (nodes scattered in memory) |
| Also implements | RandomAccess | Deque (queue and stack methods) |
How ArrayList works
An ArrayList keeps its elements in an array. Reading index i is a single step. When the array is full, add creates a new array about 1.5 times bigger and copies everything over; because this happens rarely, adding at the end is still O(1) on average. Inserting at index 0 shifts every element one place to the right.
If you know roughly how many elements are coming, new ArrayList<>(10_000) avoids the repeated copying.
How LinkedList works
A LinkedList stores each element in its own node that points to the previous and the next node. Adding or removing at either end only changes a few links. But to reach index 500 it has to walk node by node, and each node costs extra memory for the two links.
Same code, both lists
Because both implement List, you can write code against the interface and pick the implementation in one place.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class SameInterface {
static void work(List<String> list) {
list.add("b");
list.add("c");
list.add(0, "a"); // insert at the start
list.remove("c");
list.set(1, "B");
System.out.println(list.getClass().getSimpleName() + " " + list + " size=" + list.size());
}
public static void main(String[] args) {
work(new ArrayList<>());
work(new LinkedList<>());
}
}Output
ArrayList [a, B] size=2 LinkedList [a, B] size=2
LinkedList as a queue, and why ArrayDeque is better
LinkedList also implements Deque, so it can act as a queue or a stack. ArrayDeque does the same job with less memory and better speed, and it is the class Java's own documentation recommends.
import java.util.ArrayDeque;
import java.util.Deque;
public class QueueAndStack {
public static void main(String[] args) {
Deque<String> queue = new ArrayDeque<>();
queue.offer("first");
queue.offer("second");
queue.offer("third");
System.out.println(queue.poll()); // FIFO: first in, first out
Deque<String> stack = new ArrayDeque<>();
stack.push("bottom");
stack.push("middle");
stack.push("top");
System.out.println(stack.pop()); // LIFO: last in, first out
System.out.println(stack.peek());
}
}Output
first top middle
Common mistakes
- Choosing
LinkedList"because inserts are O(1)". Finding the position is O(n), so inserts in the middle are not faster in practice. - Looping over a
LinkedListwithfor (int i = 0; i < list.size(); i++) list.get(i). Everygetwalks the list, making the loop O(n²). Use a for-each loop. - Using the old
StackorVectorclasses. They are synchronized and outdated; useArrayDequeandArrayList. - Declaring variables as
ArrayList<String> list. DeclareList<String> listso the implementation can change in one place.
Frequently asked questions
Which is faster, ArrayList or LinkedList?
ArrayList for almost everything: index access, iteration and adding at the end. LinkedList wins only for frequent add and remove at the very start or through an iterator in the middle.
Is LinkedList a doubly linked list?
Yes. Each node links to both the previous and the next node, so it can be walked in both directions and used as a deque.
When should I use LinkedList in Java?
Rarely. If you need a queue or stack, use ArrayDeque. If you need a list, use ArrayList. LinkedList fits only when you insert and remove a lot through a ListIterator.
Learn it step by step
Practise this, step by step
Short lessons with quizzes, practice and interview questions, from your first program to job-ready.