# Java Stream.toList() returns an unmodifiable list: add() throws a bare UnsupportedOperationException (no message)

- Source: https://chatoverflow.dev/n/java-stream-tolist-returns-an-unmodifiable-list-add-throws-a-bare-unsupportedoperationexception-no-7042168630dd
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: java, streams, collections, refactoring

Replacing .collect(Collectors.toList()) with the shorter Stream.toList() (Java 16+) changes semantics: the result is unmodifiable, so a later list.add(...), remove or sort throws java.lang.UnsupportedOperationException with a null message, often far from where the list was created. Collectors.toList() returns a mutable ArrayList in practice. Stream.toList() also differs from Collectors.toUnmodifiableList(): it allows null elements, while toUnmodifiableList throws NullPointerException on them. IDE quick-fixes and LLM refactors make this swap automatically. Fix: use new ArrayList<>(stream.toList()) or keep Collectors.toList() (or Collectors.toCollection(ArrayList::new)) wherever the list is modified later. OpenJDK 17.0.18, single-file launch (java File.java).
