Quick revision
The short answer
Stream gatherers, final in Java 24, let us write our own intermediate operations and plug them into a pipeline with stream.gather(gatherer), the way collectors let us write our own terminal operations. The JDK ships several in the Gatherers class: windowFixed(n) groups elements into lists of n, windowSliding(n) produces overlapping windows, fold combines everything into one result while scan emits each running result, and mapConcurrent(n, f) applies a function on virtual threads with at most n calls in flight while keeping the original order.
What to remember
In this answer, we saw that stream gatherers, final in Java 24, make intermediate operations extensible: we write or pick a Gatherer and apply it with stream.gather. The built-ins cover the common cases: windowFixed for batches, windowSliding for overlapping windows, fold for a single accumulated value and scan for running results, and mapConcurrent for applying a blocking function on virtual threads with a concurrency limit while keeping encounter order. Together they replace many cases where we used to collect into a list and start over, or give up on streams entirely.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Stream gatherers, final in Java 24, let us write our own intermediate operations and plug them into a pipeline with stream.gather(gatherer), the way collectors let us write our own terminal operations. The JDK ships several in the Gatherers class: windowFixed(n) groups elements into lists of n, windowSliding(n) produces overlapping windows, fold combines everything into one result while scan emits each running result, and mapConcurrent(n, f) applies a function on virtual threads with at most n calls in flight while keeping the original order.
Why this question is asked
For years, the set of intermediate operations was fixed: map, filter, flatMap, distinct, sorted, limit and a few more. Anything else, such as grouping elements into batches of 100 or computing running totals, meant collecting into a list and starting over, or abandoning the stream for a loop. Gatherers close that gap, and they come up in interviews because they are one of the more useful recent additions to Java. In this answer, we will recap where gatherers fit in a pipeline, then walk through each built-in gatherer with its output, and finish with how they are used in practice.
1A Quick Recap: Intermediate and Terminal Operations
A stream pipeline has a source, zero or more intermediate operations and one terminal operation. Intermediate operations such as map and filter transform the stream and are lazy; the terminal operation, such as toList or collect, produces the result and makes everything run.
Collectors, added in Java 8, made the terminal end extensible: we can write our own collector and pass it to collect. Until Java 24 there was no equivalent at the intermediate stage. A gatherer is exactly that: an object that describes a custom intermediate operation. It can keep state between elements, emit zero, one or many outputs per input, stop the stream early, and run at the end to flush whatever it still holds. We use one by calling stream.gather(gatherer). Gatherers were a preview feature in Java 22 and 23 and became final in Java 24, so they are available without flags on Java 25, the current LTS.
2Grouping Into Batches: windowFixed
Gatherers.windowFixed(n) collects elements into lists of n and emits each list when it is full. The last list may be shorter if the elements run out.
Notice the second example: batching database updates or API calls is the most common real use. Before gatherers we had to collect everything into a list and slice it by index, which lost laziness and needed extra code.
3Overlapping Windows: windowSliding
Gatherers.windowSliding(n) emits a window of n elements, then moves forward by one element and emits again, so consecutive windows overlap.
Sliding windows are what we need for moving averages, detecting trends in readings, or comparing each element with its neighbours. The second example shows an edge case worth remembering: when the input is shorter than the window, we get a single window with fewer elements, so code that assumes every window has exactly n elements should check the size.
4Accumulating: fold and scan
Gatherers.fold(initial, folder) works like reduce but as an intermediate step: it combines all elements into one value and emits that single value at the end. Gatherers.scan(initial, scanner) does the same work but emits every intermediate result, which is a running total.
The initial value is given as a Supplier, so each evaluation gets a fresh starting state. fold is useful when the reduced value must keep flowing through later intermediate operations rather than end the pipeline. scan is the one people reach for most: running balances, cumulative counts, progress percentages. Both are sequential; they do not run in parallel.
5Concurrent Mapping: mapConcurrent
Gatherers.mapConcurrent(maxConcurrency, mapper) applies the mapper to elements concurrently, each call on its own virtual thread, with at most maxConcurrency calls running at the same time. The results come out in the original encounter order, not in the order the calls finish.
| Gatherer | Emits | Example output |
|---|---|---|
windowFixed(3) | non-overlapping lists of up to 3 | [1,2,3], [4,5,6], [7] |
windowSliding(3) | overlapping lists of 3 | [1,2,3], [2,3,4], [3,4,5] |
fold(() -> 0, sum) | one final accumulated value | [10] |
scan(() -> 0, sum) | every running value | [1, 3, 6, 10] |
mapConcurrent(20, f) | f(x) per element, in order | up to 20 calls at once on virtual threads |
java.util.stream.Gatherers and are used with stream.gather(...).This fills a gap that parallel streams handled badly. Parallel streams run on the small shared common pool and should not do blocking I/O. mapConcurrent uses virtual threads, which are cheap to block, and the limit protects the service we call. Because order is kept, a slow element delays the output of the elements after it, which is worth knowing when calls vary a lot in duration.
6Follow-up questions
How is a gatherer different from a collector?
A collector runs at the end of the pipeline and produces one final result; it consumes every element and cannot stop the stream early. A gatherer sits in the middle, passes elements on to the next stage, can keep state across elements, and can stop the stream early. Gatherers can also be chained with andThen.
Can I write my own gatherer?
Yes. Gatherer is an interface with an initializer for state, an integrator that receives each element and may push results downstream, an optional combiner for parallel use, and a finisher that runs at the end. Factory methods such as Gatherer.of and Gatherer.ofSequential build one from lambdas, so a simple custom gatherer is a few lines.
Do gatherers work with parallel streams?
A gatherer can support parallel evaluation if it provides a combiner. Several built-ins, and every gatherer made with ofSequential, run sequentially: in a parallel pipeline the stages before and after may still run in parallel, but the gatherer itself processes elements in order on one thread.
7Conclusion
In this answer, we saw that stream gatherers, final in Java 24, make intermediate operations extensible: we write or pick a Gatherer and apply it with stream.gather. The built-ins cover the common cases: windowFixed for batches, windowSliding for overlapping windows, fold for a single accumulated value and scan for running results, and mapConcurrent for applying a blocking function on virtual threads with a concurrency limit while keeping encounter order. Together they replace many cases where we used to collect into a list and start over, or give up on streams entirely.
Open with
Gatherers are to intermediate operations what collectors are to terminal ones. Before Java 24 the intermediate operations were fixed, map, filter, flatMap and so on, and anything else meant collecting to a list or writing a loop.
Then, if they want more
A gatherer is a custom intermediate operation that can keep state across elements, emit zero or many outputs per input, stop early, and flush at the end, and we apply it with stream.gather. They were preview in 22 and 23 and final in 24, so on Java 25 they are standard.
The JDK ships several in the Gatherers class. windowFixed of n groups elements into lists of n, with a shorter last list, which is great for batching, like one database call per 100 ids. windowSliding of n gives overlapping windows, one, two, three then two, three, four, which is what a moving average needs. fold is like reduce but in the middle of the pipeline and emits one final value, while scan emits every running value, so scan with a sum over one to four gives one, three, six, ten.
And mapConcurrent of n and a function runs the function on virtual threads with at most n in flight and keeps the encounter order, which makes it a reasonable way to fan out blocking calls, unlike parallel streams on the common pool.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 22, Java 25, Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What are stream gatherers, final in Java 24? Name some built-in gatherers and what they...": https://scrivio-interview-library.netlify.app/java/streams/c3dcdf3b4c/