Java List Interface
In this page:
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?
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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));
}
}
Login to try C/C++/Java/PHP code in the editor
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
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 try C/C++/Java/PHP code in the editor
- Calling
list.remove(int)when you meant to remove anIntegerobject, so it removes by index. - Using
List.of()and then modifying it, which throwsUnsupportedOperationException. - Reading an index beyond the size, which throws
IndexOutOfBoundsException.
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting