0
0
JavaComparisonBeginner · 4 min read

ArrayList vs LinkedList in Java: Key Differences and Usage

ArrayList stores elements in a dynamic array, offering fast random access but slower insertions/removals in the middle. LinkedList uses a doubly linked list, providing faster insertions/removals but slower random access.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of ArrayList and LinkedList based on key factors.

FactorArrayListLinkedList
Data StructureResizable arrayDoubly linked list
Access TimeFast (O(1))Slow (O(n))
Insertion/DeletionSlow in middle (O(n))Fast in middle (O(1))
Memory UsageLess (stores elements only)More (stores node pointers)
Use CaseFrequent access, less modificationFrequent insertions/deletions
⚖️

Key Differences

ArrayList stores elements in a continuous array, which allows quick access by index because it calculates the element's position directly. However, inserting or deleting elements in the middle requires shifting elements, which takes more time.

LinkedList stores elements as nodes linked by pointers. Each node points to the next and previous nodes, so accessing an element by index requires walking through nodes one by one, making it slower. But adding or removing elements in the middle is faster because it only changes pointers without shifting elements.

Memory-wise, LinkedList uses more space because each node stores extra pointers, while ArrayList only stores the elements themselves. This difference affects performance and memory use depending on your needs.

⚖️

Code Comparison

Here is how you add and print elements using ArrayList in Java.

java
import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        
        // Insert in the middle
        list.add(1, "Blueberry");
        
        // Print all elements
        for (String fruit : list) {
            System.out.println(fruit);
        }
    }
}
Output
Apple Blueberry Banana Cherry
↔️

LinkedList Equivalent

Here is the same example using LinkedList in Java.

java
import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        
        // Insert in the middle
        list.add(1, "Blueberry");
        
        // Print all elements
        for (String fruit : list) {
            System.out.println(fruit);
        }
    }
}
Output
Apple Blueberry Banana Cherry
🎯

When to Use Which

Choose ArrayList when you need fast access to elements by index and your program mostly reads data without many insertions or deletions in the middle. It is ideal for scenarios like storing and accessing a list of items frequently.

Choose LinkedList when your program frequently adds or removes elements in the middle or at the ends, and random access speed is less important. It works well for queues or when you need efficient insertions and deletions.

Key Takeaways

Use ArrayList for fast random access and fewer modifications.
Use LinkedList for frequent insertions and deletions.
ArrayList uses less memory than LinkedList.
Accessing elements by index is faster in ArrayList.
LinkedList is better for queue-like operations.