Question
I usually write Java lists like this:
List<String> names = new ArrayList<>();
I use the List interface as the variable type so I can switch implementations later if needed.
In Java, when should LinkedList be used instead of ArrayList, and when is ArrayList the better choice?
Short Answer
By the end of this page, you will understand the practical differences between ArrayList and LinkedList in Java, including how they store data, how that affects performance, and which one is usually the better default. You will also learn the common operations where each structure performs well, the mistakes beginners make when choosing between them, and how this choice shows up in real Java codebases.
Concept
ArrayList and LinkedList are both Java implementations of the List interface, but they store data in very different ways.
ArrayList stores elements in a dynamic array.
- Elements are kept in contiguous memory-like array slots.
- Access by index is fast.
- Appending to the end is usually fast.
- Inserting or removing in the middle can be expensive because later elements must shift.
LinkedList stores elements as a chain of nodes.
- Each node stores the value and links to neighboring nodes.
- Access by index is slow because Java must walk through the chain.
- Adding or removing near the ends can be efficient.
- It has more memory overhead because each element needs node objects and links.
In real programming, the important question is not just "which one can do this operation faster in theory?" but also "what operations will my code do most often?"
As a rule of thumb:
- Use
ArrayListby default. - Use
LinkedListonly when you specifically need frequent insertions/removals at the beginning or through a list iterator, or when queue/deque-style operations are central.
Why ArrayList is usually the default:
- Fast random access with
get(index) - Better cache locality in practice
Mental Model
Think of ArrayList as a row of numbered lockers.
- Every item has a position.
- If you want item 7, you go directly to locker 7.
- If you insert something in the middle, many later items need to shift one place.
Think of LinkedList as a treasure hunt with clues.
- Each item tells you where the next item is.
- To reach item 7, you must follow the chain from the start or end.
- Removing a known item from the chain is easy: reconnect the links.
So:
ArrayListis better when you want fast direct access.LinkedListis better when you are constantly linking and unlinking elements, especially at the ends.
Another short version:
ArrayListoptimizes finding by position.LinkedListoptimizes relinking nodes once you are already there.
Syntax and Examples
Both classes implement List, so you can write code against the interface.
Basic syntax
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
List<String> arrayNames = new ArrayList<>();
List<String> linkedNames = new LinkedList<>();
Example: ArrayList for general-purpose list usage
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Ava");
names.add("Liam");
names.add("Noah");
System.out.println(names.get(1));
System.out.println(names);
}
}
Why ArrayList fits here:
- Adding to the end is efficient
- Reading by index is fast
- This is the most common style in Java applications
Step by Step Execution
Consider this ArrayList example:
List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add(1, "X");
System.out.println(items);
System.out.println(items.get(2));
Step by step:
-
itemsstarts empty.[]
-
items.add("A")Ais added at the end.[A]
-
items.add("B")Bis added at the end.[A, B]
-
items.add("C")Cis added at the end.
Real World Use Cases
When ArrayList is a good fit
ArrayList is the best choice for most list usage in Java applications:
- Returning a list of users from a service
- Storing search results
- Holding rows from a CSV file
- Collecting API response items
- Looping through values for validation or display
- Accessing items by index in UI or business logic
Example:
List<Product> products = new ArrayList<>();
This is common because most code:
- adds items,
- iterates over them,
- sometimes accesses by index,
- rarely inserts at the front repeatedly.
When LinkedList can be a good fit
Use LinkedList when your operations match its strengths:
- Frequent
addFirst()/removeFirst()operations - Frequent
addLast()/removeLast()operations - Deque-like behavior
- Insertion/removal through a
ListIteratorwhen already positioned
Example:
Real Codebase Usage
In real Java codebases, developers usually follow these patterns:
1. Default to interfaces
List<String> names = new ArrayList<>();
This keeps code flexible and makes it easier to change implementations later.
2. Default to ArrayList
Most production code uses ArrayList unless there is a clear reason not to.
Why:
- It performs well for common workloads
- It is easier to reason about
- Most lists are read often and modified less dramatically than people assume
3. Use LinkedList only for specific access patterns
You may see it in code that:
- behaves like a deque
- adds/removes at both ends
- manipulates elements through iterators
Example pattern:
LinkedList<String> undoStack = new LinkedList<>();
undoStack.addFirst("state1");
undoStack.addFirst("state2");
String previous = undoStack.removeFirst();
4. Prefer purpose-specific interfaces
If the collection is used as a queue:
Common Mistakes
1. Assuming LinkedList is always faster for insertions
This is one of the most common mistakes.
Broken reasoning:
- "Insertions are O(1), so
LinkedListmust be faster."
What is missed:
- You often need to traverse the list first
- That traversal is
O(n)
2. Using LinkedList when accessing by index often
Broken example:
List<String> names = new LinkedList<>();
for (int i = 0; i < names.size(); i++) {
System.out.println(names.get(i));
}
Why this is bad:
get(i)onLinkedListisO(n)- Repeating it in a loop can become very slow
Better:
for (String name : names) {
System.out.println(name);
}
Or use ArrayList if indexed access is important.
Comparisons
ArrayList vs LinkedList
| Feature | ArrayList | LinkedList |
|---|---|---|
| Backing structure | Dynamic array | Doubly linked nodes |
| Best for | General-purpose list usage | Frequent add/remove at ends |
| Access by index | Fast | Slow |
| Append to end | Usually fast | Fast |
| Insert at beginning | Slow | Fast |
| Remove from beginning | Slow | Fast |
| Memory usage | Lower | Higher |
| Iteration performance | Usually better |
Cheat Sheet
List<String> a = new ArrayList<>();
List<String> b = new LinkedList<>();
Choose ArrayList when
- You need
get(index)often - You mostly append items
- You iterate a lot
- You want the usual default choice
Choose LinkedList when
- You frequently add/remove at the start or end
- You need deque-like operations
- You are using a
ListIteratorto insert/remove while traversing
Performance summary
ArrayList.get(i)-> O(1)LinkedList.get(i)-> O(n)ArrayList.add(end)-> amortized O(1)LinkedList.addFirst()-> O(1)LinkedList.removeFirst()-> O(1)- Insert/remove in middle often requires traversal first
Best default
- Use
List<T> list = new ArrayList<>();
Better for queues/deques
FAQ
Should I use ArrayList or LinkedList by default in Java?
Use ArrayList by default. It is usually faster and more memory-efficient for common application code.
Is LinkedList ever better than ArrayList?
Yes. It can be a better fit when you frequently add or remove elements at the beginning or end, or when you use it as a deque.
Why is LinkedList.get(index) slow?
Because a linked list cannot jump directly to an index. It must follow links from one node to the next until it reaches that position.
Is LinkedList good for queues?
It works, but ArrayDeque is usually a better choice for queue and deque behavior.
Why do many developers write List<String> x = new ArrayList<>();?
Because programming to the interface keeps code less coupled to one implementation and easier to change later.
Is Big-O enough to choose between them?
No. Big-O is useful, but real performance also depends on memory layout, traversal costs, and actual usage patterns.
Does LinkedList use more memory than ArrayList?
Mini Project
Description
Build a small Java program that models a task processor and compares two kinds of collection usage: a general task list and a queue of pending jobs. This project demonstrates that ArrayList is a strong default for ordinary list storage, while queue-like operations are better expressed with a queue/deque structure rather than forcing everything into one list type.
Goal
Create a program that stores tasks in an ArrayList, processes waiting jobs with a deque, and prints the results clearly.
Requirements
- Create a general task list using the
Listinterface with anArrayListimplementation. - Add at least four task names and print one task by index.
- Create a pending job queue using
ArrayDeque. - Add at least three jobs to the queue and process them from the front.
- Print the remaining contents of both collections at the end.
Keep learning
Related questions
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Choosing a @NotNull Annotation in Java: Validation vs Static Analysis
Learn how Java @NotNull annotations differ, when to use each one, and how to choose between validation, IDE hints, and static analysis tools.
Convert a Java Stack Trace to a String
Learn how to convert a Java exception stack trace to a string using StringWriter and PrintWriter, with examples and common mistakes.