Video in progress
What are Stream Gatherers in Java 24 and what problem do they solve?
A gatherer is an intermediate operation you write yourself, the intermediate-stage equivalent of a Collector. It was a preview in Java 22–23 and became final in Java 24 (JEP 485). Things like sliding windows, grouping into batches and stateful filtering can be done without breaking the stream.
📖 Answer
Before Gatherers, you could extend the stream API only on the terminal side (a custom Collector). To write intermediate steps like "send in batches of 100" or "drop consecutive duplicates", you had to collect the stream into a collection and open a stream again.
Now there's stream.gather(gatherer), and the JDK includes ready-made ones:
// splitting into chunks of 3: [1,2,3], [4,5,6], [7]
Stream.of(1, 2, 3, 4, 5, 6, 7)
.gather(Gatherers.windowFixed(3))
.toList();
// sliding window: [1,2], [2,3], [3,4]
Stream.of(1, 2, 3, 4).gather(Gatherers.windowSliding(2)).toList();
// map with bounded concurrency (on virtual threads)
ids.stream()
.gather(Gatherers.mapConcurrent(10, id -> client.fetch(id)))
.toList();
mapConcurrent is especially interesting: it runs I/O calls in parallel with no more than 10 at a time and preserves order. It solves a parallel stream's I/O problem quite cleanly.
You write your own with Gatherer.of(...) or Gatherer.ofSequential(...): an initializer (state), an integrator (per element, calling downstream.push for the next stage), and an optional combiner and finisher. The integrator can short-circuit by returning false.
A practical point: this works only on Java 24+. For teams that follow LTS releases, that means Java 25 LTS. Before that, I don't recommend using it in a codebase with a preview flag.
Sign up to mark studied questions and save your progress.