List Interface in Java

Last Updated : 24 Nov, 2026

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

list_interface
Hierarchy of List 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.

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

Java
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

add(E e)

Adds an element at the end.

add(int index, E e)

Inserts an element at a specified index.

addAll(Collection<? extends E> c)

Adds all elements from another collection.

get(int index)

Returns the element at the specified index.

set(int index, E e)

Replaces an element and returns the previous element.

remove(int index)

Removes and returns the element at the specified index.

remove(Object o)

Removes the first matching element.

indexOf(Object o)

Returns the first matching index or -1.

lastIndexOf(Object o)

Returns the last matching index or -1.

contains(Object o)

Checks whether an element exists.

size()

Returns the number of elements.

isEmpty()

Checks whether the list is empty.

clear()

Removes all elements.

listIterator()

Returns a bidirectional iterator.

sort(Comparator<? super E> c)

Sorts the list using the specified comparator.

equals(Object o)

Compares lists for equal size and corresponding elements.

hashCode()

Returns the hash code calculated according to the List contract.

Comment