Open In App

Collections unmodifiableList() method in Java with Examples

Last Updated : 08 Nov, 2025
Comments
Improve
Suggest changes
9 Likes
Like
Report

The unmodifiableList() method of the java.util.Collections class is used to create a read-only view of a specified list. It is particularly useful when you want to share a list with other parts of the application but prevent them from modifying its contents.

Any attempt to modify the returned list, either directly or through its iterator, will result in an UnsupportedOperationException.

Basic unmodifiableList() usage

Java
import java.util.*;

public class Example1 {
    public static void main(String[] args) {
        // Create an ArrayList of Characters
        List<Character> list = new ArrayList<>();
        list.add('A');
        list.add('B');

        System.out.println("Original List: " + list);

        // Create an unmodifiable view
        List<Character> immutableList = Collections.unmodifiableList(list);

        System.out.println("Unmodifiable List: " + immutableList);
    }
}

Output
Original List: [A, B]
Unmodifiable List: [A, B]
  • The unmodifiableList() method returns a read-only view of the original list.
  • You can still modify the original list, and those changes will reflect in the unmodifiable view.

Method Definition

public static <T> List<T> unmodifiableList(List<? extends T> list)

Parameters

  • list: The list for which an unmodifiable view is to be returned.

Return Value: Returns an unmodifiable view of the specified list. Any modification attempt on this view will throw an exception.

Key Points

  • The returned list reflects any changes made to the original list (since it’s a view, not a copy).
  • It does not allow structural modification such as add(), remove(), or clear().
  • The returned list is serializable if the original list is serializable.
  • If the provided list implements RandomAccess, the returned list also implements it.

Attempting to Modify the Unmodifiable List

Java
import java.util.*;

public class Example2 {
    public static void main(String[] args) {
        try {
            // Create a modifiable list
            List<Character> list = new ArrayList<>();
            list.add('X');
            list.add('Y');

            System.out.println("Initial List: " + list);

            // Create an unmodifiable view
            List<Character> immutableList = Collections.unmodifiableList(list);

            // Attempt to modify the unmodifiable list
            System.out.println("\nTrying to modify the unmodifiable list...");
            immutableList.add('Z'); // Throws exception
        } 
        catch (UnsupportedOperationException e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}

Output
Initial List: [X, Y]

Trying to modify the unmodifiable list...
Exception thrown: java.lang.UnsupportedOperationException

When you try to add, remove, or alter the unmodifiable list, Java throws an UnsupportedOperationException, indicating that modification is not allowed.

Reflecting Changes from Original List

Java
import java.util.*;

public class Example3 {
    public static void main(String[] args) {
        List<String> cities = new ArrayList<>();
        cities.add("Delhi");
        cities.add("Mumbai");

        List<String> unmodifiableCities = Collections.unmodifiableList(cities);

        System.out.println("Original List: " + cities);
        System.out.println("Unmodifiable List: " + unmodifiableCities);

        // Modify the original list
        cities.add("Chennai");

        System.out.println("\nAfter modifying the original list:");
        System.out.println("Original List: " + cities);
        System.out.println("Unmodifiable List: " + unmodifiableCities);
    }
}

Output
Original List: [Delhi, Mumbai]
Unmodifiable List: [Delhi, Mumbai]

After modifying the original list:
Original List: [Delhi, Mumbai, Chennai]
Unmodifiable List: [Delhi, Mumbai, Chennai]

Even though the returned list is unmodifiable, it is still backed by the original list. Therefore, any modification to the original list is reflected in the unmodifiable view.

When to Use unmodifiableList()

  • When exposing internal collections to external modules while ensuring they cannot be modified.
  • When creating immutable views for read-only operations.
  • When working in multi-layered applications where one layer should not alter another layer’s data.

Explore