The List interface in Java is part of the java.util package and represents an ordered collection of elements. It allows duplicate values and provides index-based access to elements.
- Maintains the order in which elements are inserted, unless they are explicitly reordered.
- Supports duplicate elements and allows multiple null values in implementations such as ArrayList.
- Provides bidirectional traversal using ListIterator.
import java.util.ArrayList;
import java.util.List;
public class Geeks{
public static void main(String[] args){
// Create a List using ArrayList
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("DSA");
languages.add("C++");
for (String language : languages){
System.out.println(language);
}
}
}
Output
Java Python DSA C++
Explanation: An ArrayList object is created using a List reference. The add() method inserts elements, and the enhanced for loop displays them in their stored order.es.
Syntax
List<Type> list = new ArrayList<>();
Declaration
public interface List<E> extends SequencedCollection<E>
Here, E represents the type of elements stored in the List.
Note: Since Java 21, List extends SequencedCollection, which extends Collection. Before Java 21, List directly extended Collection.
Hierarchy of List Interface
Since Java 21, the List interface extends SequencedCollection, which extends the Collection interface.

Common Implementation Classes of List
The common implementation classes of the List interface are:
- ArrayList uses a resizable array and provides fast index-based access. Inserting or removing elements in the middle requires shifting elements.
- LinkedList uses a doubly linked list. Insertion or deletion is efficient when the required node or iterator position is already known, but locating a position can require traversal.
- Vector is a legacy synchronized resizable-array implementation.
- Stack extends Vector and provides LIFO operations. ArrayDeque is generally preferred for modern stack usage.
Operations on List
The List interface supports adding, accessing, updating, searching, and removing elements using index-based methods.
1. Adding, Accessing, Updating, and Removing Elements
Example: Program demonstrates the main operations on a List.
import java.util.ArrayList;
import java.util.List;
public class Geeks{
public static void main(String[] args){
List<String> list = new ArrayList<>();
// Add elements
list.add("Java");
list.add("Python");
list.add("C++");
// Insert at index 1
list.add(1, "DSA");
System.out.println("After Adding: " + list);
// Access an element
System.out.println("Element at Index 0: " + list.get(0));
// Update an element
list.set(2, "JavaScript");
System.out.println("After Updating: " + list);
// Remove an element by index
list.remove(1);
System.out.println("After Removing: " + list);
// Check an element
System.out.println("Contains C++: " + list.contains("C++"));
}
}
Output
After Adding: [Java, DSA, Python, C++] Element at Index 0: Java After Updating: [Java, DSA, JavaScript, C++] After Removing: [Java, JavaScript, C++] Contains C++: true
Explanation:
- add() inserts elements at the end or at a specified index.
- get() retrieves an element using its index.
- set() replaces an existing element.
- remove() deletes an element.
- contains() checks whether an element exists.
Note: Valid insertion indices range from 0 to the current list size. An invalid index causes an
IndexOutOfBoundsException.
2. Searching and Iterating Over List Elements
The List interface provides indexOf() and lastIndexOf() for searching. It also supports iteration using loops and ListIterator.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Geeks{
public static void main(String[] args){
List<Integer> numbers = new ArrayList<>(Arrays.asList(10, 20, 30, 20));
// Search for an element
System.out.println("First Index: " + numbers.indexOf(20));
System.out.println("Last Index: " + numbers.lastIndexOf(20));
// Iterate over the list
for (int num : numbers){
System.out.print(num + " ");
}
}
}
Output
First Index: 1 Last Index: 3 10 20 30 20
Explanation: indexOf() returns the first matching index, while lastIndexOf() returns the last matching index. Both return -1 if the element is absent. The enhanced for loop traverses the elements in order.
Methods of List Interface
Method | Description |
|---|---|
Adds an element at the end. | |
Inserts an element at a specified index. | |
Adds all elements from another collection. | |
Returns the element at the specified index. | |
Replaces an element and returns the previous element. | |
Removes and returns the element at the specified index. | |
Removes the first matching element. | |
Returns the first matching index or -1. | |
Returns the last matching index or -1. | |
Checks whether an element exists. | |
Returns the number of elements. | |
Checks whether the list is empty. | |
Removes all elements. | |
Returns a bidirectional iterator. | |
Sorts the list using the specified comparator. | |
Compares lists for equal size and corresponding elements. | |
Returns the hash code calculated according to the List contract. |