Stream Gatherers in Java: The Stream API Finally Learned to Window, Scan, and Fold

Since Java 8, the Stream API has had an asymmetry: terminal operations were extensible — collect(Collector) lets you define any summary you like — but intermediate operations were a fixed menu. If you wanted windows, running totals, or distinct-by-key in the middle of a pipeline, you either broke out of the pipeline into a loop, or bent a Collector into doing an intermediate operation’s job. Stream Gatherers close that gap. Stream.gather(Gatherer) is to intermediate operations what collect(Collector) is to terminal operations — that framing is JEP 485’s own, and it is the fastest way to understand the feature. ...

2026-11-16 · 19 min · Ramesh

Java Streams

JAVA Streams Sum of Elements Given a list of integers, compute the sum of all numbers. // using sum List<Integer> arr = Arrays.asList(1, 4, 5, 6, 22, 3, 90, 89, 2, 1, 3, 4, 55, 6); int sum = arr.stream() .mapToInt(Integer::intValue) .sum(); // or int sum3 = arr.stream() .reduce((a,b) -> a + b) .get(); // or int sum2 = arr.stream() .reduce(0, (total, n) -> total + n, (total1, total2) -> total1 + total2); Average of Elements Given a list of integers, compute the average of all numbers. ...

2024-07-29 · 3 min · Ramesh