The answer is: it depends on what you’re doing. But generally:
🏎️ ArrayList is faster than LinkedList in most real-world use cases.
Let’s break it down by operations:
🔍 1. Access by Index (get/set)
Operation | ArrayList | LinkedList |
---|---|---|
get(index) | ✅ O(1) (fast) | ❌ O(n) (slow) |
set(index) | ✅ O(1) (fast) | ❌ O(n) (slow) |
🏆 Winner: ArrayList
🔍 2. Add or Remove at End
Operation | ArrayList | LinkedList |
---|---|---|
add() at end | ✅ O(1) amortized | ✅ O(1) |
removeLast() | ✅ O(1) | ✅ O(1) |
🏆 Tie, but ArrayList
might be faster due to lower overhead.
🔍 3. Add or Remove in the Middle or Beginning
Operation | ArrayList | LinkedList |
---|---|---|
add(0, element) | ❌ O(n) (shift) | ✅ O(1) |
remove(0) | ❌ O(n) (shift) | ✅ O(1) |
add(index, element) | ❌ O(n) | ⚠️ O(n) (need to find node first) |
remove(index) | ❌ O(n) | ⚠️ O(n) (same) |
So if you’re inserting/removing frequently at the head, LinkedList
can be faster.
🧠 4. Iteration Performance
Aspect | ArrayList | LinkedList |
---|---|---|
Sequential scan | ✅ Fast (good cache locality) | ❌ Slower (poor cache locality) |
🏆 Winner: ArrayList
📦 5. Memory Usage
ArrayList
: Just an array + size → low overhead.LinkedList
: Each node storesdata + next + prev
→ higher overhead.
✅ When to use ArrayList:
- You need fast random access.
- You’re mainly adding/removing at the end.
- You’re dealing with large data and care about memory.
✅ When to use LinkedList:
- You need to insert/delete at the beginning often.
- You’re implementing a queue or stack.
- You rarely need random access.