How to Fix java.lang.UnsupportedOperationException in Java Lists

beginnerโ˜• Java2026-05-01| Java Development Kit (JDK) 8, 11, 17, 21+ on any Operating System (Windows, macOS, Linux)

Error Message

java.lang.UnsupportedOperationException
#java#collections#list#unsupportedoperationexception

TL;DR: The Quick Fix

The java.lang.UnsupportedOperationException usually happens because you are trying to add or remove elements from a list that is either fixed-size or immutable. The fastest way to fix this is to wrap your existing list in a new ArrayList.

// Instead of this (throws exception on modification):
List<String> list = Arrays.asList("A", "B", "C");

// Do this (creates a mutable copy):
List<String> mutableList = new ArrayList<>(Arrays.asList("A", "B", "C"));
mutableList.add("D"); // Now this works!

Identifying the Root Cause

In Java, not all lists are created equal. While they all implement the List interface, their underlying implementations determine whether you can modify them. The java.lang.UnsupportedOperationException is thrown when you call a method (like add(), remove(), or clear()) that the specific list implementation does not support.

Common Culprit 1: Arrays.asList()

When you use Arrays.asList("item1", "item2"), it returns a fixed-size list. This list is backed by the original array. You can change the value of an existing element using set(), but you cannot change the size of the list. Since add() and remove() would change the size, they throw the exception.

List<String> items = Arrays.asList("Apple", "Banana");
items.set(0, "Apricot"); // This is fine
items.add("Cherry");    // Throws java.lang.UnsupportedOperationException

Common Culprit 2: List.of() (Java 9 and later)

Introduced in Java 9, List.of() creates a truly immutable list. Unlike Arrays.asList(), you cannot even use set() to change an existing value. Any attempt to alter the list in any way results in an error.

List<String> items = List.of("Apple", "Banana");
items.set(0, "Orange"); // Throws java.lang.UnsupportedOperationException
items.add("Cherry");    // Throws java.lang.UnsupportedOperationException

Common Culprit 3: Collections.unmodifiableList()

Sometimes you receive a list from a library or another part of your code that has been wrapped to prevent modification. This is a common pattern to protect internal data structures.

Reliable Fix Approaches

1. Create a Mutable Copy (Recommended)

The most robust solution is to pass the fixed or immutable list into the constructor of a java.util.ArrayList. This creates a completely new list containing the same elements, but with the full functionality of a standard dynamic array.

List<String> fixedList = Arrays.asList("one", "two");
List<String> workingList = new ArrayList<>(fixedList);

workingList.add("three");
workingList.remove("one");

2. Use Java 8 Stream API

If you are processing data using streams and want to ensure the resulting list is mutable, use Collectors.toCollection(ArrayList::new) instead of the default Collectors.toList() (which in some Java versions or implementations might return an immutable list).

List<String> mutableList = someDataSource.stream()
    .filter(s -> s.startsWith("A"))
    .collect(Collectors.toCollection(ArrayList::new));

3. Proper Initialization for Java 7 and older

If you aren't using modern helper methods, stick to the classic initialization to avoid surprises:

List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");

Verification Steps

To confirm your fix is working, perform a simple modification test immediately after list creation. If the following code executes without crashing, your list is mutable:

try {
    List<String> myTestList = // ... your list creation logic
    myTestList.add("test_element");
    myTestList.remove("test_element");
    System.out.println("Success: List is mutable.");
} catch (UnsupportedOperationException e) {
    System.err.println("Failure: List is still immutable/fixed-size!");
}

Key Takeaways

- **Arrays.asList** is fixed-size (supports `set`, but not `add/remove`).
- **List.of** is strictly immutable (supports no changes).
- **ArrayList** constructor is your best friend for converting restricted lists into workable ones.
- Always check the documentation of third-party APIs to see if the lists they return are intended to be modified.

Related Error Notes