ArrayList vs LinkedList in Java
In this lesson: ArrayList vs LinkedList compared — internal structure, speed of get/add/remove — table and examples. In Hindi & English.
Two kinds of List
ArrayList and LinkedList are both Lists — ordered collections that allow duplicates — but they store data differently. This affects their speed: ArrayList is faster for access by index, while LinkedList is faster for inserting or removing in the middle.
ArrayList
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
System.out.println(list.get(0)); // fast index access
ArrayList stores items in a resizable array. Getting an item by index (get(0)) is very fast. It is the default List for most everyday needs.
LinkedList
import java.util.LinkedList;
LinkedList<String> list = new LinkedList<>();
list.add("A");
list.addFirst("Start"); // fast add at the front
System.out.println(list);
LinkedList stores items as connected nodes. Adding or removing at the start or middle is fast because no elements need shifting. It also has handy methods like addFirst.
How they store data
- ArrayList — one continuous array; each item sits at a numbered slot.
- LinkedList — separate nodes, each holding a value plus a link to the next node.
This underlying difference is the reason for their different strengths.
Speed differences
- Access by index: ArrayList is fast (jumps straight there); LinkedList is slow (walks node by node).
- Insert/remove in the middle: LinkedList is fast (just relink); ArrayList is slow (shifts elements).
- Memory: ArrayList is lighter; LinkedList stores extra links.
Comparison
| Point | ArrayList | LinkedList |
|---|---|---|
| Stores as | Array | Linked nodes |
| Access by index | Fast | Slow |
| Insert/remove middle | Slow | Fast |
| Memory | Less | More |
| Best for | Reading/access | Frequent insert/remove |
Common mistakes
- Using LinkedList by default. ArrayList is faster and lighter for most cases.
- Random access on LinkedList.
get(i)in a loop is slow — avoid it. - Ignoring the operation you do most. Choose based on access vs insert/remove.
- Forgetting both are Lists. They share the List methods, so switching is easy.
Summary
- Both are Lists; the difference is how they store data.
- ArrayList uses an array — fast access, less memory.
- LinkedList uses nodes — fast insert/remove in the middle.
- Default to ArrayList; use LinkedList for frequent middle changes.
- Avoid index access in a loop on a LinkedList.
Next, revise the big picture in the Collections Framework, or compare maps in HashMap vs Hashtable.
