In Java, the List interface (part of java.util) represents an ordered collection that allows duplicate elements and provides positional access via indexes.

Here’s a breakdown of the main List implementations you’ll encounter:

1. ArrayList

  • Backed by: Dynamic array.
  • Performance:
    • Fast random access (O(1) for get() and set()).
    • Slower insertions/removals in the middle (O(n) due to shifting elements).
  • When to use: When you need frequent reads and rare middle insertions.
  • Example:
import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        System.out.println(list.get(1)); // Banana
    }
}

2. LinkedList

  • Backed by: Doubly linked list.
  • Performance:
    • Fast insertions/removals at the beginning or middle (O(1) if you have the node reference).
    • Slower random access (O(n) for get()).
  • When to use: When you need frequent insertions/removals and sequential access.
  • Example:
import java.util.LinkedList;
import java.util.List;

public class LinkedListExample {
    public static void main(String[] args) {
        List<String> list = new LinkedList<>();
        list.add("Dog"); list.add("Cat");
        list.add(1, "Bird");
        System.out.println(list);
        // [Dog, Bird, Cat]
    }
}

3. Vector

  • Backed by: Dynamic array (like ArrayList).
  • Thread-safety: Synchronized (safe for multi-threaded use, but slower).
  • When to use: Rarely — only if you specifically need a legacy synchronized list.
  • Example:
import java.util.List;
import java.util.Vector;

public
class VectorExample {
    public static void main(String[] args) {
        List<Integer> list = new Vector<>();
        list.add(10);
        list.add(20);
        System.out.println(list);
        // [10, 20]
    }
}

4. CopyOnWriteArrayList (from java.util.concurrent)

  • Backed by: Copy-on-write array.
  • Thread-safety: Yes — safe for concurrent reads/writes.
  • Performance:
    • Reads are very fast.
    • Writes are expensive (copying the entire array).
  • When to use: In multi-threaded environments with many reads and few writes.
  • Example:
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListExample {
    public static void main(String[] args) {
        List<String> list = new CopyOnWriteArrayList<>();
        list.add("One"); list.add("Two");
        System.out.println(list);
        // [One, Two]
    }
}

Summary Table

Implementation
Backing Structure
Thread-Safe
Best For
ArrayList
Dynamic array
No
Fast random access, few middle inserts
LinkedList
Doubly linked list
No
Frequent inserts/removes, sequential access
Vector
Dynamic array
Yes
Legacy synchronized list
CopyOnWriteArrayList
Dynamic array (copy on write)
Yes
Many reads, few writes in multi-threaded apps
← Back to Learning Journey