Collect a stream straight into an immutable list.
List<Integer> doubled = Stream.of(1, 2, 3)
.map(n -> n * 2)
.collect(Collectors.toUnmodifiableList());
System.out.println(doubled);
try { doubled.add(99); } catch (UnsupportedOperationException e) {
System.out.println("unmodifiable");
}
[2, 4, 6]
unmodifiable
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-26