Stop 08 · Collections & GenericsTopics · 2/10

ArrayList vs LinkedList

Part of the Java Roadmap

In short

Both store an ordered list, so why are there two? Because they are built very differently inside, and that changes which operations are fast. The good news: ArrayList wins in most real code.

ArrayList is backed by a resizable array with fast random access. LinkedList is a doubly linked list with cheap insertion at the ends but slow index access. ArrayList is the right default in most cases.

The jaadu shuffles people along a row of cinema seats on one side, while on the other side it hooks a new coach onto a little toy train.

Think of it like this

Numbered seats vs a train of coaches

ArrayList is a cinema row with numbered seats: jump straight to seat 7, but squeezing a friend in the middle means everyone shifts one seat right. LinkedList is a train where each coach is hooked to the next: adding a coach at either end is easy, but finding coach 7 means walking through the first six.

Watch it happen

Inserting into the middle of an ArrayList

Aarav
0
Diya
1
get(1)
Kabir
2
3

1/4An ArrayList is a real array underneath. get(1) jumps straight to Diya.

The big ideas

What you need to know

ArrayList: fast get(i)

Index lookup is O(1) because elements sit side by side in memory. Adding at the end is also fast on average.

ArrayList: costly middle inserts

Inserting or removing near the front shifts every later element, which is O(n).

LinkedList: cheap ends

It is a doubly linked list, so addFirst, addLast, removeFirst and removeLast are O(1).

LinkedList: slow get(i)

To reach index i it walks node by node. Each node also uses extra memory for two links.

See it in code

Walk through the code

java
1import java.util.ArrayList;2import java.util.LinkedList;3import java.util.List;4 5public class ListDemo {6    public static void main(String[] args) {7        List<String> queue = new ArrayList<>(List.of("Aarav", "Diya", "Kabir"));8        System.out.println(queue.get(1));   // Diya, instant lookup by index9 10        queue.add(1, "Meera");              // shifts Diya and Kabir right11        System.out.println(queue);          // [Aarav, Meera, Diya, Kabir]12 13        LinkedList<String> stops = new LinkedList<>();14        stops.addFirst("Bandra");15        stops.addLast("Andheri");16        stops.removeFirst();                // cheap at either end17        System.out.println(stops);          // [Andheri]18    }19}

1/3ArrayList finds index 1 directly, no walking needed.

The same List interface, two very different engines. Reach for ArrayList unless you have measured a reason not to.

Save yourself the pain

Common mistakes

Oops

Choosing LinkedList because "inserts are O(1)".

Do this instead

Inserts are only cheap once you are already at the node. Finding the position is O(n), so ArrayList is usually faster overall.

Oops

Using LinkedList as a queue or stack.

Do this instead

ArrayDeque does that job faster and with less memory.

Oops

Calling list.get(i) inside a loop on a LinkedList.

Do this instead

Use a for-each loop or an iterator, which walks the nodes once instead of restarting every time.

Your mission

Feel the difference

~20 min

Quick check

You need to read elements by index very often. Which list should you use?

Go deeper

  1. 1Practise add, get, set, remove and contains on an ArrayList.
  2. 2Time get(i) on a large ArrayList versus a large LinkedList.
  3. 3Learn why removing items while looping with for-each throws ConcurrentModificationException.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge