Java

ArrayList vs LinkedList in Java

Compare ArrayList and LinkedList in Java by access patterns, insertion cost, memory overhead, and practical backend usage.

Quick answer

Use ArrayList by default for most Java application code. It is compact in memory, fast for indexed reads, and efficient for the most common backend pattern: build a list, iterate it, transform it, serialize it, or return it from a service.

Use LinkedList only when you have a specific operation pattern that benefits from node-based insertion or removal, and even then, measure before treating it as faster. In real backend systems, LinkedList is often slower than expected because each node allocation adds memory overhead and pointer chasing.

Main difference

ArrayList stores elements in a resizable array. That gives it fast random access because the JVM can compute the memory position from the index. When the internal array fills up, ArrayList allocates a larger array and copies the existing references.

LinkedList stores elements in nodes. Each node holds the value plus references to the previous and next node. Once code already has a node position through an iterator, insertion and removal can be cheap. But finding an index requires walking through the list.

Performance table

OperationArrayListLinkedList
Get by indexO(1)O(n)
Set by indexO(1)O(n)
Append at endUsually O(1), occasional resizeO(1)
Insert near beginningO(n), shifts elementsO(1) after position is known
Remove by indexO(n), shifts elementsO(n) to find, then O(1) unlink
Iterate all elementsFast and cache-friendlyMore pointer chasing
Memory overheadLowerHigher

The important phrase is after position is known. Many explanations say LinkedList insertion is O(1), but backend code often starts with an index, a value search, or a loop. In those cases, finding the position can dominate the cost.

Java example

List<String> users = new ArrayList<>();
users.add("alice");
users.add("bob");
users.add("charlie");

String second = users.get(1); // fast indexed access

This is the shape of many service-layer collections: collect records, map them, filter them, and serialize them. ArrayList fits this pattern well.

List<String> queue = new LinkedList<>();
queue.add("job-a");
queue.add("job-b");
queue.remove(0);

This code works, but if you need queue behavior, prefer a queue-oriented type such as ArrayDeque for most single-threaded use cases. The type communicates intent better and usually performs well.

Memory overhead matters

An ArrayList stores references in one backing array. A LinkedList stores a separate node object for each element, and every node has extra references for the previous and next links.

That overhead matters when a list grows large or appears in hot paths. More objects also create more work for the garbage collector. This is one reason LinkedList can lose in practice even when a simple Big-O table suggests an advantage.

Practical backend guidance

Choose ArrayList when:

  • You need indexed reads.
  • You mostly append and iterate.
  • You return DTO lists from services or controllers.
  • You process database result sets.
  • You want the simplest default collection choice.

Consider another structure when:

  • You need queue operations: use ArrayDeque or a Queue implementation.
  • You need priority-based removal: use PriorityQueue.
  • You need thread-safe access: choose a concurrent collection or control ownership explicitly.
  • You need frequent middle insertions and already hold iterator positions.

Common mistake

The common mistake is choosing LinkedList because “insertion is faster.” That statement is incomplete. Inserting into a LinkedList is fast only when the code already has the insertion point. If the code first calls get(index), searches for a value, or walks the list, the operation is no longer simply O(1).

Read Time Complexity of Java Collections for the broader reference table. Read Java PriorityQueue Explained when you need priority-based removal. Read HashMap vs Hashtable in Java for another Java collections tradeoff, and Big-O Notation Explained for the complexity language used in this comparison.