← 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.
Syntax
java
List<Type> list = new ArrayList<>();
list.add(item);
list.get(index);

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 so it can be used by its short name
import java.util.List;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
import java.util.LinkedList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in arrayList
		List<String> arrayList = new ArrayList<>();
		// Create a new LinkedList object and store it in linkedList
		List<String> linkedList = new LinkedList<>();
		arrayList.add("fast index access");
		linkedList.add("chain of nodes");
		// Print a line to the console
		System.out.println(arrayList);
		// Print a line to the console
		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 so it can be used by its short name
import java.util.List;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in names
		List<String> names = new ArrayList<>();
		names.add("Alice");
		names.add("Bob");
		// Print a line to the console
		System.out.println(names.get(0));
		// Print a line to the console
		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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling list.remove(int) when you meant to remove an Integer object, so it removes by index.
  2. Using List.of() and then modifying it, which throws UnsupportedOperationException.
  3. Reading an index beyond the size, which throws IndexOutOfBoundsException.

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.