Java List Interface
In this page:
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;
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);
}
}
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;
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);
}
}
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
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