Fixing the UnsupportedOperation Exception in Java: A Step-by-Step Guide

Troubleshooting and Resolving the UnsupportedOperationException in Java

Corey Duffy
Javarevisited
3 min readJan 20, 2023

--

So now I’ll just sort this list and… oh. That’s weird. UnsupportedOperation Exception. But my IDE says it’s fine.

So, what’s gone wrong?

This is actually a common exception that many developers will stumble upon, especially when working with factory methods like Arrays.asList() or List.of(). Let's take a look at how this exception occurs and, more importantly, how to fix it.

Code Example

The example below is a bit contrived but it gets the general point across. In Java, I can create a list via:

List<String> exampleList = List.of("alpha", "gamma", "beta");

If I wanted to sort this list by alphabetical order, one option to do so would be to use the .sort() method, e.g.

exampleList.sort(Comparator.comparing(Object::toString));

This looks fine, .sort() is a valid method in the List interface and your trusty IDE generally won’t flag anything wrong with what you’re trying to do. However, when you go running your program you’ll come across the roadblock that is UnsupportedOperationException.

java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:72)
at java.base/java.util.ImmutableCollections$AbstractImmutableList.sort(ImmutableCollections.java:111)

Why has this happened and what does it mean?

What is an UnsupportedOperationException?

An UnsupportedOperationException is a Runtime exception that is a member of the Java Collections Framework. It is thrown when you attempt to do something that isn’t possible for the object you’re working on. i.e. you try to perform an operation that isn’t supported by the object that you’re operating on.

But in the case of my list above, this seems to be a valid method…

Why am I seeing this exception?

Well, it turns out that the exception is being thrown because I’m trying to sort an immutable list. Therefore, I’m trying to alter something that can’t be altered.

When we create a list in Java via List.of() we’re actually creating a list that is immutable. Therefore, I can’t add new elements to the list, remove elements from it or alter it by sorting. So, the sort() operation that I’m trying to perform isn’t supported.

How do I fix it?

If I need to perform a sort(), add(), remove(), etc. operation on my list then I’ll need to make sure that the list is mutable. There are a number of ways in which I can achieve this.

I can make sure that when the list is first initialised, or before I perform any operation that will edit the list, that it is made mutable. An ArrayList in Java is one form of mutable List:

// One method of making the list mutable
List<String> mutableListExample1 = new ArrayList<>(List.of("alpha", "gamma", "beta"));

// One method of initialising a mutable list
List<String> mutableListExample2 = new ArrayList<>(){{
add("alpha");
add("gamma");
add("beta");
}};

Because my list is now mutable, I can perform a sort on its contents with no problems:

List<String> exampleList = new ArrayList<>(List.of("alpha", "gamma", "beta"));
exampleList.add("delta");
exampleList.sort(Comparator.comparing(Object::toString));
System.out.println("My sorted list: " + String.join(",", exampleList));

which will output:

My sorted list: alpha,beta,delta,gamma

Summary

In summary, an UnsupportedOperationException is thrown when you attempt to perform an operation on an object that doesn’t allow for said operation.

The most common cause for this exception is trying to make changes to an immutable list, set, map, etc.

To fix the issue, please ensure that the data structure you are trying to operate on is mutable and allows for changes to be made to its contents.

Thanks very much for taking the time to read this. Hopefully, it’s been of some help to you in understanding what the UnsupportedOperationException is and how to fix any issues surrounding it.

If you enjoyed this or found it informative, please consider reading some of my other articles and follow me on Medium. Cheers!

--

--

Corey Duffy
Javarevisited

Helping developers expand their skill set, advance their careers, or start something new | Building software for 8+ years in start-ups & large companies.