In practice ArrayList is faster for almost everything. It stores elements in a contiguous array, so get(index) is O(1) and iteration is cache-friendly. LinkedList's theoretical advantage (O(1) insert and remove in the middle) rarely helps, because you first have to walk to that position, which is O(n), and every node is a separate object.
Key Points: • ArrayList: get/set O(1); add at the end amortised O(1); insert or remove in the middle O(n) because elements shift (done with a fast System.arraycopy). • LinkedList: get(index) O(n); insert or remove is O(1) only once you already have a ListIterator at that position. • LinkedList uses much more memory: each node holds the value plus next and previous pointers (around 24 extra bytes). • LinkedList is a good fit only when you insert or remove a lot through an iterator, or use it as a Deque. Even then, ArrayDeque is usually better for queues. • Joshua Bloch, who wrote LinkedList, has said he never uses it himself.
Code Example:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ListPerformance {
static long timeGets(List<Integer> list) {
long start = System.nanoTime();
long sum = 0;
for (int i = 0; i < list.size(); i += 100) sum += list.get(i);
return (System.nanoTime() - start) / 1_000_000;
}
public static void main(String[] args) {
int n = 100_000;
List<Integer> array = new ArrayList<>();
List<Integer> linked = new LinkedList<>();
for (int i = 0; i < n; i++) { array.add(i); linked.add(i); }
long a = timeGets(array);
long l = timeGets(linked);
System.out.println("1,000 gets by index: ArrayList is faster? " + (a <= l));
// Where LinkedList shines: removing at the head
((LinkedList<Integer>) linked).removeFirst(); // O(1)
array.remove(0); // O(n): shifts 99,999 elements
System.out.println("Sizes after removing the first element: " + array.size() + ", " + linked.size());
}
}Output:
1,000 gets by index: ArrayList is faster? true Sizes after removing the first element: 99999, 99999
Good to Know: Exact timings depend on the machine and the JVM, so the example prints a comparison rather than raw milliseconds. For real measurements, use JMH (the Java Microbenchmark Harness); simple timing loops are distorted by JIT warm-up.
Interview Tip: A concise interview answer is:
"ArrayList is backed by an array, so get is O(1) and iteration is cache-friendly, while LinkedList needs O(n) to reach an index and uses extra memory for every node. LinkedList's O(1) insert and remove only helps when you're already at the position with an iterator, so I use ArrayList by default, and ArrayDeque when I need a queue."
Previous question: What is the difference between HashSet, LinkedHashSet and TreeSet?
Next question: What is the best way to iterate over a HashMap in Java?