← Back to Core Java Course | Chapter 11: Collections | Lesson 3 of 17

Java List Interface

The List interface represents an ordered collection accessed by numeric index, and is one of the three core interfaces -- alongside Set and Map -- in Java's Collections Framework.

What is the List Interface?

The List interface represents an ordered collection of elements that can be accessed by their numeric index, and is one of the three core interfaces -- alongside Set and Map -- that make up Java's Collections Framework.

Example: What is the List Interface?

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<String> names = new ArrayList<>();
		names.add("Alice");
		System.out.println(names.get(0)); // ordered, accessible by index
	}
}

List Implementations

ArrayList and LinkedList are the two most common implementations of the List interface: ArrayList stores elements in a resizable array for fast index access, while LinkedList stores them as a chain of linked nodes for fast insertion and removal.

Example: List Implementations

java
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		List<String> arrayList = new ArrayList<>();
		List<String> linkedList = new LinkedList<>();
		arrayList.add("fast index access");
		linkedList.add("chain of nodes");
		System.out.println(arrayList);
		System.out.println(linkedList);
	}
}

Common List Methods

The List interface defines common methods like add, remove, get, set, size, and contains, all of which work identically regardless of which concrete List implementation is actually being used underneath.

Example: Common List Methods

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<String> names = new ArrayList<>();
		names.add("Alice");
		names.add("Bob");
		System.out.println(names.get(0));
		System.out.println(names.contains("Bob"));
		System.out.println(names.size());
		names.remove("Alice");
		System.out.println(names);
	}
}

Ordered and Indexed Access

A List always preserves the order elements were inserted in and lets any element be retrieved or replaced directly by its zero-based index using get and set, unlike a Set which has no concept of position.

Example: Ordered and Indexed Access

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<String> names = new ArrayList<>();
		names.add("Alice");
		names.add("Bob");
		names.set(0, "Charlie"); // replace by index
		System.out.println(names.get(0));
	}
}

Allowing Duplicates

A List explicitly allows duplicate values to be stored, with each occurrence kept as its own separate entry at its own index, which is the key behavioral difference that distinguishes it from the Set interface.

Example: Allowing Duplicates

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<String> names = new ArrayList<>();
		names.add("Alice");
		names.add("Alice"); // duplicates allowed, kept as separate entries
		System.out.println(names);
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.