List Interface in Java : Complete Guide for Developers
The List interface in Java represents an ordered collection that allows duplicate elements and provides positional access. It is a part of the java.util package and extends the Collection interface. Lists are commonly used when the order of elements matters, and when you need to access elements by their index position.
Key Characteristics of List:
- Ordered Collection: Elements in a
Listare ordered, meaning they maintain the sequence in which they were inserted. - Allows Duplicates: A
Listcan contain multiple occurrences of the same element. - Indexed Access: Elements can be accessed using an integer index, with the first element at index 0.
- Null Elements:
Listimplementations allow the inclusion of null elements.
Declaration of the Java List Interface
The List interface in Java is declared as follows:
public interface List<E> extends Collection<E> { }
It represents an ordered collection that allows duplicate elements and provides positional access.
Since List is an interface, we cannot instantiate it directly. Instead, we create an instance of a class that implements it, such as ArrayList, LinkedList, or Vector.
Example:
import java.util.List;
import java.util.ArrayList;
public class ListDemo {
public static void main(String[] args) {
// Instantiate a List using ArrayList
List<String> list = new ArrayList<>(); // Using ArrayList
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println(list); //Output: [Java, Python, C++]
}
}
β Explanation:
List<String>β The generic type<String>specifies that this list will storeStringelements.new ArrayList<>()β We are creating an ArrayList instance that implements theListinterface.
Common Implementations of List:
ArrayListLinkedListVectorStackCopyOnWriteArrayList

List Implementation classes
Java provides several classes that implement the List interface, each with its own characteristics and use cases:
ArrayList: A resizable array implementation of theListinterface. It allows fast random access and is efficient for storing and accessing data. However, adding or removing elements (except at the end) can be slow due to the need to shift elements.LinkedList: A doubly-linked list implementation of theListinterface. It allows for efficient insertion or removal of elements from both ends and from the middle of the list. However, it provides slower access to elements by index compared toArrayList.Vector: An older implementation of theListinterface. It is similar toArrayListbut is synchronized, making it thread-safe. However, this synchronization comes with a performance cost.Stack: A subclass ofVectorthat represents a last-in, first-out stack of objects. It provides methods likepush(),pop(), andpeek()to operate on the stack.CopyOnWriteArrayList: A thread-safe variant ofArrayListwhere all mutative operations (likeadd,set, etc.) are implemented by making a fresh copy of the underlying array. This is useful when you have a list that is frequently read but infrequently modified.
Java List β Operations in Detail
The List interface in Java (part of the Collection Framework) represents an ordered collection that can contain duplicate elements.
Common implementations: ArrayList, LinkedList, CopyOnWriteArrayList, Vector.
1. Creating a List
import java.util.*;
public class ListCreation {
public static void main(String[] args) {
List<String> list = new ArrayList<>(); // Using ArrayList
list.add("Java");
list.add("Python");
list.add("C++");
System.out.println(list); // [Java, Python, C++]
}
}
2. Adding Elements
add(E e)β Adds element at the end.add(int index, E e)β Inserts element at a specific index.
list.add("Go"); // Adds at end
list.add(1, "Kotlin"); // Inserts at index 1
3. Accessing Elements
get(int index)β Retrieves element at given index.
System.out.println(list.get(0)); // Java
System.out.println(list.get(2)); // Kotlin
4. Updating Elements
set(int index, E e)β Replaces element at index.
list.set(2, "Rust");
System.out.println(list); // [Java, Kotlin, Rust, Go]
5. Removing Elements
remove(int index)β Removes by index.remove(Object o)β Removes first occurrence of object.clear()β Removes all elements.
list.remove("Kotlin"); // Removes element
list.remove(1); // Removes by index
list.clear(); // Removes all
6. Searching in List
contains(Object o)β Checks if element exists.indexOf(Object o)β First index of element, or-1if not found.lastIndexOf(Object o)β Last index of element.
System.out.println(list.contains("Java")); // true
System.out.println(list.indexOf("Java")); // 0
System.out.println(list.lastIndexOf("Go")); // 3
7. Iterating Over a List
- For-each Loop
for (String lang: list) {
System.out.println(lang);
}
- Iterator
Iterator < String > itr = list.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
- ListIterator (Bidirectional)
ListIterator < String > litr = list.listIterator();
while (litr.hasNext()) {
System.out.println(litr.next());
}
- Java 8+ forEach with Lambda
list.forEach(System.out::println);
8. Sorting a List
Collections.sort(list); // Natural order
list.sort(Comparator.reverseOrder()); // Reverse order
9. Sublist Operation
List < String > sub = list.subList(1, 3);
System.out.println(sub); // [Python, C++]
10. Converting List to Array
String[] arr = list.toArray(new String[0]);
System.out.println(Arrays.toString(arr));
11. Streams & Functional Operations (Java 8+)
list.stream()
.filter(lang -> lang.startsWith("J"))
.forEach(System.out::println); // Java
12. Checking if an Element is Present in a List
The List interface provides methods to check whether an element exists inside a list.
β Methods Used
contains(Object o)β Returnstrueif the list contains the specified element, elsefalse.indexOf(Object o)β Returns the first index of the element, or-1if not found.lastIndexOf(Object o)β Returns the last index of the element, or-1if not found.
Example
import java.util.*;
public class ListSearchExample {
public static void main(String[] args) {
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("C++");
languages.add("Java"); // duplicate
// 1. contains()
System.out.println(languages.contains("Java")); // true
System.out.println(languages.contains("Rust")); // false
// 2. indexOf()
System.out.println(languages.indexOf("Java")); // 0 (first occurrence)
System.out.println(languages.indexOf("Rust")); // -1 (not found)
// 3. lastIndexOf()
System.out.println(languages.lastIndexOf("Java")); // 3 (last occurrence)
}
}
Output
true
false
0
-1
3
π Summary Table of List Operations
| Operation | Method Example | Description |
|---|---|---|
| Add | add("Java"),addAll() | Insert element |
| Access | get(0) | Retrieve element |
| Update | set(1, "Kotlin") | Replace element |
| Remove | remove("Java"),removeAll(), retainAll(), clear() | Remove by value |
| Search | contains("Java"),indexOf(), lastIndexOf() | Check existence |
| Iterate | forEach(System.out::println),iterator(), listIterator(), spliterator() | Traverse list |
| Sort | Collections.sort(list) | Sort elements |
| Sublist | subList(1, 3) | Extract portion |
| Convert | toArray(), size(), isEmpty() | Convert to array, Get size of elements, Check if the list is empty |
β
In short:
The List interface in Java provides powerful operations to manage ordered collections, making it one of the most widely used data structures in Java applications.