java.lang.UnsupportedOperationException — how to fix
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 Main.main(Main.java:8)
java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)
Quick fix: Copy into a mutable collection before modifying: List<String> list = new ArrayList<>(Arrays.asList(a, b)); or new ArrayList<>(List.of(...)). For streams use .collect(Collectors.toCollection(ArrayList::new)) instead of .toList().
What it means
Collection interfaces like List and Map declare methods such as add and remove as optional. Fixed-size and immutable implementations throw UnsupportedOperationException when you call them. The top stack frame tells you which kind you have:
AbstractList.add/AbstractList.remove- a fixed-size list, usually fromArrays.asListImmutableCollections.uoe- an immutable collection fromList.of,Set.of,Map.of,List.copyOforStream.toList()Collections$UnmodifiableCollection- a read-only view fromCollections.unmodifiableListetc.
Cause 1: Arrays.asList is fixed-size
List<String> names = Arrays.asList("a", "b");
names.add("c"); // UnsupportedOperationException
names.set(0, "z"); // allowed - it's fixed-size, not immutable
Arrays.asList is a view backed by the array: you can change elements but not the size. Copy it:
List<String> names = new ArrayList<>(Arrays.asList("a", "b"));
names.add("c");
Cause 2: List.of / Map.of / Set.of are immutable
List<Integer> nums = List.of(1, 2, 3);
nums.remove(0); // UnsupportedOperationException
Same fix:
List<Integer> nums = new ArrayList<>(List.of(1, 2, 3));
Map<String, Integer> m = new HashMap<>(Map.of("a", 1));
Cause 3: Stream.toList() (Java 16+)
List<String> upper = names.stream().map(String::toUpperCase).toList();
upper.add("X"); // UnsupportedOperationException
Stream.toList() returns an unmodifiable list. If you need to modify the result:
List<String> upper = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toCollection(ArrayList::new));
Cause 4: an API returned a read-only collection
Getters that return Collections.unmodifiableList(...), JPA/Hibernate results in some cases, Kotlin listOf passed to Java, or Collections.emptyList() all reject modification. Don't modify what you didn't create; copy it:
List<Order> orders = new ArrayList<>(customer.getOrders());
orders.add(newOrder);
Returning read-only views is deliberate: it stops callers from changing an object's internal state behind its back. If you own the class and callers should change it, add a method like addOrder(...) instead of exposing the list.
Tip: removing elements
On a mutable copy, removeIf is the cleanest way to delete matching elements:
List<String> list = new ArrayList<>(source);
list.removeIf(String::isEmpty);
If you get ConcurrentModificationException instead, see that page.
Last updated 2026-12-01.