# java.lang.UnsupportedOperationException：不可变列表无法修改

- **ID:** `java/unsupported-operation-exception-immutable-list`
- **领域:** java
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

尝试修改通过 List.of()、Collections.unmodifiableList() 或 Stream.toList() 等方法创建的不可变集合，这些集合不支持 add、remove 或 set 操作。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Java 9 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## 解决方案

1. ```
   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+).
   ```
3. ```
   If the list is returned from a library and you must modify it, wrap it in a new ArrayList immediately after receiving it.
   ```

## 无效尝试

- **** — Catching and ignoring the exception does not fix the logic; the modification is silently skipped, leading to data inconsistency. (90% 失败率)
- **** — Adding @SuppressWarnings does not change the runtime behavior; the exception still occurs. (100% 失败率)
- **** — 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% 失败率)
