5 · Intermediate · lesson 29 of 41
Streams API
Declarative pipelines over collections.
Java
import java.util.stream.*;
int sum = List.of(1,2,3,4,5,6).stream()
.filter(n -> n % 2 == 0) // 2, 4, 6
.mapToInt(Integer::intValue)
.sum(); // 12
Map<Boolean,List<Integer>> parts = IntStream.rangeClosed(1, 10)
.boxed()
.collect(Collectors.partitioningBy(n -> n % 2 == 0));NOTE
Streams are lazy — nothing happens until a terminal operation (sum, collect, forEach).
Loading 3D scene…
Key takeaways
- ✓Stages: source → intermediate ops → terminal op.
- ✓Streams are single-use; create a new one each time.