Quick Answer
Handle immutable collection modifications.
Understanding the Issue
Thrown when trying to modify immutable collections like those from Arrays.asList() or Collections.unmodifiableList().
The Problem
This code demonstrates the issue:
Java
Error
List<String> fixed = Arrays.asList("a", "b");
fixed.add("c"); // Throws
The Solution
Here's the corrected code:
Java
Fixed
// Solution: Create mutable copy
List<String> mutable = new ArrayList<>(fixed);
mutable.add("c");
// Or using Java 10+ copyOf
List<String> copy = List.copyOf(fixed); // Immutable
Key Takeaways
Be aware of immutable collections returned by convenience methods.