Why do I get an UnsupportedOperationException when trying to remove an element from a List?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with java.util.List in Java, an UnsupportedOperationException is a runtime exception that might be thrown if an attempt is made to modify a list that does not support modification. This article explores why this exception occurs, along with some practical examples and remedies.
Understanding UnsupportedOperationException
The UnsupportedOperationException is thrown to indicate that the requested operation is not supported. In the context of lists, this usually occurs when trying to modify an immutable list or a list that explicitly does not support a particular operation. This is oftentimes the case with lists returned by certain standard library methods or wrappers that do not support mutation, such as Collections.unmodifiableList or as results from certain APIs like Arrays methods.
Common Scenarios Leading to UnsupportedOperationException
1. Using Arrays.asList
When arrays are converted to Lists via the Arrays.asList method, the resulting List is a fixed-size list directly backed by the array. You can modify the elements directly if they are objects (not primitives), and it will reflect in the original array. However, operations that would change the size of the list, like add or remove, are unsupported.
Example:
The remove method tries to alter the size of the List, hence the exception.
2. Using Collections.unmodifiableList
When you wrap a List with Collections.unmodifiableList, any attempt to modify it by add/remove methods will result in UnsupportedOperationException.
Example:
3. Immutable Collections from Recent Java Versions
Starting from Java 9, methods like List.of and List.copyOf create immutable lists. Altering these lists in any manner similar to the above will throw an UnsupportedOperationException.
Example:
Dealing with UnsupportedOperationException
To avoid UnsupportedOperationException, you should be aware of the characteristics of the List implementation you are using. If mutation is necessary, ensure that you are not using an immutable or fixed-size list. Here’s how you can handle lists that might throw this exception:
- Check the documentation: Always check the Java documentation to see if the List returned by a method supports add or remove operations.
- Create a mutable copy: If you receive an immutable list and need a modifiable list, create a new one from it.
Example:
- Alternative data structures: If your requirements involve frequent add/removal, consider using other data structures like
ArrayListorLinkedListthat support dynamic sizes.
Summary Table
| Scenario | List Creation Method | Supports Removal? | Example |
| Using Array-backed List | Arrays.asList(T... a) | No | Arrays.asList("one", "two").remove(1) |
| Dealing with Immutable Collections | Collections.unmodifiableList(List<?> list) | No | Collections.unmodifiableList(new ArrayList<>()).add("one") |
| Java 9+ Immutable Collections | List.of(E... elements) | No | List.of("apple", "pie").remove("pie") |
Conclusion
The UnsupportedOperationException serves as an essential mechanism for enforcing the immutability of collections in certain contexts in Java. Understanding when and why this exception is thrown helps in building robust and error-free applications. When manipulating collections, always consider the specific operations supported by your list's implementation and handle scenarios where modifications are intended by opting for a suitable alternative that supports mutable operations.

