How to Solve Java List UnsupportedOperationException?
Last Updated : 09 Mar, 2021
The UnsupportedOperationException is one of the common exceptions that occur when we are working with some API of list implementation. It is thrown to indicate that the requested operation is not supported.
This class is a member of the Java Collections Framework.
All java errors implement the java.lang.Throwable interface or are inherited from another class. The hierarchy of this Exception is-
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.UnsupportedOperationException
Syntax:
public class UnsupportedOperationException extends RuntimeException
The main reason behind the occurrence of this error is the asList method of java.util.Arrays class returns an object of an ArrayList which is nested inside the class java.util.Arrays. ArrayList extends java.util.AbstractList and it does not implement add or remove method. Thus when this method is called on the list object, it calls to add or remove method of AbstractList class which throws this exception. Moreover, the list returned by the asList method is a fixed-size list therefore it cannot be modified.
The below example will result in UnsupportedOperationException as it is trying to add a new element to a fixed-size list object
Java import java.util.Arrays; import java.util.List; public class Example { public static void main(String[] args) { String str[] = { "Apple", "Banana" }; List<String> l = Arrays.asList(str); System.out.println(l); // It will throw java.lang.UnsupportedOperationException l.add("Mango"); } }
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) at Example.main(Example.java:14)
We can solve this problem by using a mutable List that can be modified such as an ArrayList. We create a List using Arrays.asList method as we were using earlier and pass that resultant List to create a new ArrayList object.
Java import java.util.ArrayList; import java.util.List; import java.util.*; public class Example { public static void main(String[] args) { String str[] = { "Apple", "Banana" }; List<String> list = Arrays.asList(str); List<String> l = new ArrayList<>(list); l.add("Mango"); // modify the list for(String s: l ) System.out.println(s); } }
Similar Reads
Java Program to Update the List Items In Java, Lists are dynamic collections that allow modifications, such as updating elements. We can use the set method to update the elements in the List. This method replaces the element at the given index with a new value, allowing modification of list contents. In this article, we will learn to up
2 min read
How to Fix java.util.NoSuchElementException in Java? An unexpected, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exceptions are caused by our program and these are recoverable. Suppose if our program requirement is to read data from the remote file locating in the U.S.A. At runtime, if a remote file
4 min read
How to Test Java List Interface Methods using Mockito? Mockito is an open-source testing framework used for unit testing of Java applications. It plays a vital role in developing testable applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in Unit Testing. The List interface in
5 min read
Fix "java.lang.NullPointerException" in Android Studio Hey Geeks, today we will see what NullPointerException means and how we can fix it in Android Studio. To understand NullPointerException, we have to understand the meaning of Null. What is null? "null" is a very familiar keyword among all the programmers out there. It is basically a Literal for Refe
4 min read
How to Solve java.lang.NoSuchMethodError in Java? A java.lang.NoSuchMethodError as the name suggests, is a runtime error in Java which occurs when a method is called that exists at compile-time, but does not exist at runtime. The java.lang.NoSuchMethodError can occur in case application code is partially compiled, or in case an external dependency
3 min read