# java.lang.UnsupportedOperationException: Immutable list cannot be modified

- **ID:** `java/unsupported-operation-exception-immutable-list`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

An attempt was made to modify a collection that was created as immutable (e.g., via List.of(), Collections.unmodifiableList(), or Stream.toList()), which does not support add, remove, or set operations.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Java 9 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## Workarounds

1. **If modification is needed, create a mutable copy of the immutable list using the ArrayList constructor, then modify the copy.** (95% success)
   ```
   If modification is needed, create a mutable copy of the immutable list using the ArrayList constructor, then modify the copy.
   ```
2. **Use Stream.collect(Collectors.toList()) to create a mutable list instead of Stream.toList() (which returns an immutable list in Java 16+).** (90% success)
   ```
   Use Stream.collect(Collectors.toList()) to create a mutable list instead of Stream.toList() (which returns an immutable list in Java 16+).
   ```
3. **If the list is returned from a library and you must modify it, wrap it in a new ArrayList immediately after receiving it.** (90% success)
   ```
   If the list is returned from a library and you must modify it, wrap it in a new ArrayList immediately after receiving it.
   ```

## Dead Ends

- **** — Catching and ignoring the exception does not fix the logic; the modification is silently skipped, leading to data inconsistency. (90% fail)
- **** — Adding @SuppressWarnings does not change the runtime behavior; the exception still occurs. (100% fail)
- **** — Using ArrayList constructor on the immutable list creates a mutable copy, but if the code continues to use the original reference, the error persists. (60% fail)
