Stream.toList()
Terminal toList() replaces the verbose collect(Collectors.toList()).
Code Comparison
✕ Java 8
List<String> result = stream
.filter(s -> s.length() > 3)
.collect(Collectors.toList());
✓ Java 16+
List<String> result = stream
.filter(s -> s.length() > 3)
.toList();
Why the modern way wins
7 chars vs 24
.toList() replaces .collect(Collectors.toList()).
Immutable
The result list cannot be modified.
Fluent
Reads naturally at the end of a pipeline.
Old Approach
Collectors.toList()
Modern Approach
.toList()
Since JDK
16
Difficulty
beginner
JDK Support
Stream.toList()
Available
Widely available since JDK 16 (March 2021)
How it works
Stream.toList() returns an unmodifiable list. It's equivalent to .collect(Collectors.toUnmodifiableList()) but much shorter. Note: the result is immutable, unlike Collectors.toList().