A general extension point for intermediate stream operations, so you can write windowing, folding and custom stateful operations that compose like built-ins.
The problem it solved
The set of intermediate operations was fixed. Anything not provided — sliding windows, fixed batches, take-while-with-state — meant leaving the stream, collecting to a list, and looping.
How you did it before
Collect to a list, loop over it with index arithmetic, then build a new stream. The pipeline was broken in the middle purely because the operation you needed did not exist.
// Requires JDK 24 — not compiled on this build.
var windows = Stream.of(1, 2, 3, 4, 5)
.gather(Gatherers.windowFixed(2))
.toList();
System.out.println(windows);
[[1, 2], [3, 4], [5]]Journey: Preview in 22 (JEP 461) and 23, final in 24.
Asked as
- What problem do stream gatherers solve?
- How is a gatherer different from a collector?
- How would you batch a stream into fixed-size groups before 24?