JavaList Interface

List Interface

List<E> is the Collections Framework interface for an ordered collection that allows duplicate elements and gives you direct, index-based access — the same role an array plays, but resizable and backed by much richer functionality.
Key Characteristics
  • Elements keep the order they were inserted in

  • Duplicate values are allowed — the same value can appear at multiple indices

  • Every element has a numeric position, starting at index 0

  • Elements can be inserted, retrieved, updated, or removed by that index

Main Implementations
List is an interface — you never write new List<>(). Instead you pick one of its implementing classes, most commonly ArrayList (backed by a resizable array, fast random access) or LinkedList (backed by linked nodes, fast insertion/removal at the ends). The dedicated ArrayList and LinkedList pages cover each implementation's internals and trade-offs in detail.

Declaring by interface, choosing an implementation

Java
import java.util.List;
import java.util.ArrayList;

public class ListDemo {
    public static void main(String[] args) {
        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Apple"); // duplicates are allowed

        System.out.println(fruits);
        System.out.println(fruits.get(0));
        System.out.println(fruits.size());
    }
}
[Apple, Banana, Apple]
Apple
3
Common List Methods

Method

Purpose

add(element)

Appends an element to the end of the list

get(index)

Returns the element at the given index

set(index, element)

Replaces the element at the given index

remove(index)

Removes the element at the given index

indexOf(element)

Returns the first index of an element, or -1 if absent

contains(element)

Returns true if the element exists anywhere in the list

size()

Returns the number of elements currently in the list

Common List methods in action

Java
import java.util.List;
import java.util.ArrayList;

public class ListMethodsDemo {
    public static void main(String[] args) {
        List<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");

        colors.set(1, "Yellow");
        System.out.println(colors);

        System.out.println(colors.indexOf("Blue"));
        System.out.println(colors.contains("Purple"));

        colors.remove(0);
        System.out.println(colors);
    }
}
[Red, Yellow, Blue]
2
false
[Yellow, Blue]
Warning
remove(int index) and remove(Object element) are overloaded methods with very different behavior. list.remove(1) removes the element at index 1, while list.remove(Integer.valueOf(1)) removes the element equal to the value 1. This is a well-known gotcha forList<Integer>, where list.remove(1) always means "remove by index," never "remove the value 1."
List.of() — Quick Immutable Lists (Java 9+)
For a small, fixed list you don't intend to modify, List.of(...) creates an immutable list in a single expression, with no need to create an ArrayList and add elements one at a time.

List.of()

Java
import java.util.List;

public class ImmutableListDemo {
    public static void main(String[] args) {
        List<String> days = List.of("Mon", "Tue", "Wed");
        System.out.println(days);
        days.add("Thu"); // throws UnsupportedOperationException
    }
}
[Mon, Tue, Wed]
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)
Warning
A list returned by List.of() is truly immutable — calling add(), remove(), or set() on it always throws UnsupportedOperationException, even if you only try to change it once. If you need a list you can modify, use new ArrayList<>(List.of(...)) to copy it into a mutable one first.