Attempting to modify an unmodifiable or read-only Collection structure.
Thrown to indicate that the requested collection mutation operation (such as add, remove, or set) is not supported by the underlying implementation.
Invoking mutator methods on list instances returned by List.of(), Collections.unmodifiableList(), or Arrays.asList() (which does not support resize operations).
1import java.util.*;2 3public class Main {4 public static void main(String[] args) {5 List<String> readonlyList = List.of("A", "B");6 readonlyList.add("C"); // Throws UnsupportedOperationException!7 }8}1import java.util.*;2 3public class Main {4 public static void main(String[] args) {5 List<String> readonlyList = List.of("A", "B");6 7 // Fix: Wrap in a mutable ArrayList instance8 List<String> mutableList = new ArrayList<>(readonlyList);9 mutableList.add("C"); // Allowed10 System.out.println(mutableList);11 }12}Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
List.of() returns an immutable list instance. Mutator methods like add() or remove() throw UnsupportedOperationException at runtime because modification of this list structure is prohibited.