List Interface in Java

Naveen AnamaneniUpdated · 4 min read

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

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 null elements.

Implementation Classes

ClassInternal structureBest for
ArrayListA resizable arrayFast access by index; the default choice
LinkedListA doubly linked listMany insertions and removals at the ends
VectorA synchronized resizable arrayLegacy code only
StackExtends VectorLegacy code only; use ArrayDeque

Methods of List

MethodDescription
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.

ListDemo.java▶ Try it
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));
    }
}
Output
[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:

RemoveTrap.java▶ Try it
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);
    }
}
Output
[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

  1. Easy: Create a list of five subjects, insert one at index 2, replace the last one, and print the index of "Physics".
  2. Harder: Write a method that returns a new list with the elements of a List<String> in reverse order, without using Collections.reverse.

Quick check: List interface

4 questions
1What index does the first element of a list have?
2Does a List allow duplicates?
3On a List [10, 20, 30], what does remove(1) remove?
4What does indexOf return when the element isn't found?

Key Takeaways

Was this tutorial helpful?

Lesson 258 of 482