Streams process collections declaratively: keep, transform, then gather the results.
List<Integer> nums = List.of(1, 2, 3, 4, 5, 6);
List<Integer> squaredEvens = nums.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println(squaredEvens);
[4, 16, 36]
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