package org.code2care.exceptions;
import java.util.Arrays;
import java.util.List;
public class UnsupportedOperationExceptionExample1 {
public static void main(String[] args) {
List<String> nameList = Arrays.asList("Sam", "Alex", "Mike", "Annie", "Jane");
nameList.add("Andrew");
}
}
Example 2: LinkedList
public class UnsupportedOperationExceptionExample2 {
public static void main(String[] args) {
List<String> nameList = LinkedList.of("Sam", "Alex", "Mike", "Annie", "Jane");
nameList.add("Andrew");
}
}
Example 3: Set - HashSet
public class UnsupportedOperationExceptionExample3 {
public static void main(String[] args) {
Set<Integer> numbers = Set.of(10,20,30,40,50);
numbers.add(44);
}
}
All the three above examples will give java.lang.UnsupportedOperationException because when you make use of Arrays.asList() or the Collection.of() methods you get an immutable collection, thus you cannot modify such collection by adding, deleting our updating it.

Fix: java.lang.UnsupportedOperationException
The fix is simple, just create the collection as you do use the new keyword and add elements using the add method(s).
Example:public class FixUnsupportedOperationException {
public static void main(String[] args) {
List<String> nameList = new ArrayList();
nameList.add("Sam");
nameList.add("Alex");
nameList.add("Mike");
nameList.add("Annie");
nameList.add("Jane");
nameList.add("Andrew"); //No issue
}
}
Have Questions? Post them here!
- Error: Can not find the tag library descriptor for
- Create a Database Table using JDBC PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- Java Jackson with Maven Example
- [fix] Java JDBC ConnectException: Connection refused
- Spring Boot: Transactions Management with JDBCTemplate Example
- Java Get Current Date for a Specific Time Zone
- What are the 8 Primitive Data Types in Java
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Maven Eclipse (M2e) No archetypes currently available
- How to Sort a LinkedList in Java
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Split a String in Java with Examples
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Truncate table using Java JDBC Example
- Java: Generate random numbers within a range
- Parse XML file in Java using DOM Parser
- How to get Client IP address using Java Code Example
- JDBCTemplate Querying Examples with Spring Boot 3
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- String Boot + Redis - SET and GET String Commands Examples
- Setting up Spring Boot 3 + Maven + MySQL + JDBC Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- Java Split String by Spaces
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- MongoDB Hello World! Example - 2022
- PHP Fatal error : Call to a member function bind_param() on a non-object - PHP
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python] - Python
- How to Uninstall Brew on Mac - MacOS
- hibernate.cfg.xml Configuration and Mapping xml Example - Java
- Pass data between two Android Activities and access it using Intent - Android
- How to detect Operating System using Java code - Java
- How to enable Do Not Disturb mode for Notification Center in Mac OS X 10.10 Yosemite - Mac-OS-X