[Solved] java.lang.UnsupportedOperationException in Java with examples

In this post, I will be sharing how to fix the java.lang.UnsupportedOperationException in Java with examples. According to Oracle docs, it is thrown to indicate that the requested operation is not supported. It can be classified as an unchecked exception since it extends RuntimeException.

Read Also: [Fixed] java.lang.reflect.InvocationTargetException in Java

Lets's dive deep into the topic:

[Fixed] java.lang.UnsupportedOperationException

As always, first, we will produce the java.lang.UnsupportedOperationException exception before moving on to the solution.

1. Producing the exception while removing the element from the List


java.lang.UnsupportedOperationException can be easily produced while removing the element from the List as shown below in the example:

import java.util.List;
import java.util.Arrays;

public class UnsupportedOperationExceptionExample {
    public static void main(String args[]) {
        String[] str = {"21", "22", "23", "24", "25"};
List<String> list = Arrays.asList(str);
list.remove(2); System.out.println(list); } }


Output:
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.base/java.util.AbstractList.remove(AbstractList.java:167)


Solution


In the above example, we are getting java.lang.UnsupportedOperationException because of using the asList() method of java.util.Arrays class. Arrays.asList() method returns a fixed-size list backed by the specified array. In simple words, the Arrays.asList() method returns a fixed-size list object which means that elements cannot be added to or removed from the list. So operations like adding or removing can not be performed on such kinds of Lists. The solution to getting rid of java.lang.UnsupportedOperationException is given below:

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;

public class UnsupportedOperationExceptionExample {
    public static void main(String args[]) {
        String[] str = {"21", "22", "23", "24", "25"};
List<String> list = new ArrayList<>(Arrays.asList(str));
list.remove(2); System.out.println(list); } }


Output:
[21, 22, 24, 25]


2. Producing the exception while adding the element to the List


Just like the above example, if we replace the remove() method with add() method then also we will get java.lang.UnsupportedOperationException as shown below in the example:

import java.util.List;
import java.util.Arrays;

public class UnsupportedOperationExceptionExample2 {
    public static void main(String args[]) {
        String[] strArr = {"11", "12", "13", "14", "15"};
List<String> list = Arrays.asList(strArr);
list.add("16"); System.out.println(list); } }


Output:
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.base/java.util.AbstractList.add(AbstractList.java:153)
    at java.base/java.util.AbstractList.add(AbstractList.java:111)


Solution


As stated above, Arrays.asList() method returns a fixed-size list object which means that elements cannot be added to or removed from the list. The solution to getting rid of this exception is shown below:

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;

public class UnsupportedOperationExceptionExample2 {
    public static void main(String args[]) {
        String[] strArr = {"11", "12", "13", "14", "15"};
List<String> list = new ArrayList<>(Arrays.asList(strArr));
list.add("16"); System.out.println(list); } }


Output:
[11, 12, 13, 14, 15, 16]


The other cases where this exception can occur are:

1. When trying to remove elements using an iterator

2. When trying to add, remove or set elements using ListIterator

That's all for today. Please mention in the comments if you have any questions related to java.lang.UnsupportedOperationException in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry