DEV Community

KIRAN RAJ
KIRAN RAJ

Posted on

ArrayList vs LinkedList in Java

ArrayList vs LinkedList in Java (Easy Explanation)

When learning Java Collections, one common doubt is:
“Should I use ArrayList or LinkedList?”

Both are part of the List interface. Both store data in order.
But internally, they work in completely different ways — and that changes performance.


ArrayList – How it works

Image

Image

Image

Image

Image

An ArrayList is a resizable array.

  • Elements are stored next to each other (continuous memory)
  • Each element has an index (0, 1, 2…)
  • You can directly access any element using the index

Example:

ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);

System.out.println(list.get(1)); // 20
Enter fullscreen mode Exit fullscreen mode

Real-world feel:
Like selecting a song by number in a playlist — instant access


LinkedList – How it works

Image

Image

Image

Image

Image

Image

A LinkedList is a chain of nodes connected together.

Each node contains:

  • Data

  • Reference to next node

  • Reference to previous node

  • Elements are not stored together

  • To access an element, it moves step by step

Example:

LinkedList<Integer> list = new LinkedList<>();
list.add(10);
list.add(20);
list.addFirst(5);

System.out.println(list);
Enter fullscreen mode Exit fullscreen mode

Real-world feel:
Like standing in a queue — you move one step at a time


Key Differences

Feature ArrayList LinkedList
Storage Continuous Non-continuous
Access Fast (O(1)) Slow (O(n))
Insert/Delete Slow (shifting) Fast (no shifting)
Memory Less More

Easy Way to Remember

  • ArrayList → Like seats in a theater
    If one person leaves, others shift

  • LinkedList → Like people holding hands
    Remove one, just reconnect


When to Use

Use ArrayList when:

  • You read/access data frequently
  • You need fast get() operations
  • Insert/delete is rare

Use LinkedList when:

  • You frequently insert/delete
  • Especially at beginning or middle
  • Access speed is not important

Final Thought

There is no “better” one.

  • Need fast access → go with ArrayList
  • Need easy modification → go with LinkedList

It all depends on how you use your data.

Top comments (0)