List Interface in Java
The List interface in Java is a child interface of Collection. It is used to store an ordered sequence of elements, where every element has a position called an index, starting from 0. A list allows duplicate elements and null values. In this lesson, you will learn the features of List, its methods, and its main implementation classes.
Declaration
public interface List<E> extends SequencedCollection<E>List extends SequencedCollection (Java 21), which extends Collection. So a list has all Collection methods, plus methods that work with positions.
Features of List
- Ordered: elements stay in the order they were inserted.
- Index-based: any element can be read or changed by its index.
- Allows duplicates: the same value can appear many times.
- Allows null: most implementations accept
nullelements.
Implementation Classes
| Class | Internal structure | Best for |
|---|---|---|
ArrayList | A resizable array | Fast access by index; the default choice |
LinkedList | A doubly linked list | Many insertions and removals at the ends |
Vector | A synchronized resizable array | Legacy code only |
Stack | Extends Vector | Legacy code only; use ArrayDeque |
Methods of List
| Method | Description |
|---|---|
void add(int index, E e) | It is used to insert an element at the given index. |
E get(int index) | It returns the element at the given index. |
E set(int index, E e) | It replaces the element at the given index and returns the old one. |
E remove(int index) | It removes and returns the element at the given index. |
int indexOf(Object o) | It returns the first index of the element, or -1. |
int lastIndexOf(Object o) | It returns the last index of the element, or -1. |
List<E> subList(int from, int to) | It returns a view of part of the list (to is excluded). |
void sort(Comparator<? super E> c) | It sorts the list. |
static List<E> of(E... elements) | It returns an unmodifiable list. |
Example: Working with a List
Let's see an example of index-based methods.
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
List<String> names = new ArrayList<>(
List.of("Navika", "Srihitha", "Advitha"));
names.add(1, "Anamaneni");
names.set(0, "NAVIKA");
System.out.println(names);
System.out.println("Index of Advitha: " + names.indexOf("Advitha"));
System.out.println("Element at 2: " + names.get(2));
System.out.println("Sub list: " + names.subList(1, 3));
}
}[NAVIKA, Anamaneni, Srihitha, Advitha]
Index of Advitha: 3
Element at 2: Srihitha
Sub list: [Anamaneni, Srihitha]Explanation: add(1, "Anamaneni") inserts a name at index 1 and shifts the others to the right. set(0, ...) replaces the first element. indexOf finds "Advitha" at index 3, and subList(1, 3) returns the elements at indexes 1 and 2.
The remove Trap with Integers
List<Integer> has two remove methods: remove(int index) and remove(Object o). Passing an int calls the index version:
import java.util.ArrayList;
import java.util.List;
public class RemoveTrap {
public static void main(String[] args) {
List<Integer> marks = new ArrayList<>(List.of(10, 20, 30));
marks.remove(1);
System.out.println(marks);
marks.remove(Integer.valueOf(10));
System.out.println(marks);
}
}[10, 30]
[30]Explanation: remove(1) removed the element at index 1 (the 20), not the value 1. To remove the value 10, wrap it as an object: remove(Integer.valueOf(10)).
Common Mistakes
Calling remove(5) on a List<Integer> to remove the value 5. Use remove(Integer.valueOf(5)).
Changing a subList view and the original list in turn. The view becomes invalid; copy it with new ArrayList<>(list.subList(a, b)).
Using an index equal to size(). Valid indexes go from 0 to size() - 1.
Practice
- Easy: Create a list of five subjects, insert one at index 2, replace the last one, and print the index of "Physics".
- Harder: Write a method that returns a new list with the elements of a
List<String>in reverse order, without usingCollections.reverse.
Key Takeaways
Was this tutorial helpful?