Quick revision
The short answer
I would use a custom gatherer when an intermediate step needs state across elements, such as removing consecutive duplicates or keeping the first element per key, and the pipeline should stay lazy and able to stop early, which a collector cannot do because it only runs at the end and always consumes everything. Before writing one, check the built-ins in Gatherers; when we do write one, we test empty streams, early termination and, if it claims parallel support, combining. And because gatherers need Java 24 or later, shared code can only use them once every consumer runs that version.
What to remember
In this answer, we saw that gatherers fill the gap between collectors, which only run at the end and cannot short-circuit, and custom Spliterators or loops, which are verbose or do not compose. We decide on a custom gatherer by checking the built-ins first, confirming the step needs state across elements and benefits from laziness, and putting it in a shared library when several teams need it. We test empty streams, early termination and, for gatherers that claim it, parallel combining. And because gatherers need Java 24 or later, adopting them in shared code is tied to the minimum Java version of its consumers.
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.
I would use a custom gatherer when an intermediate step needs state across elements, such as removing consecutive duplicates or keeping the first element per key, and the pipeline should stay lazy and able to stop early, which a collector cannot do because it only runs at the end and always consumes everything. Before writing one, check the built-ins in Gatherers; when we do write one, we test empty streams, early termination and, if it claims parallel support, combining. And because gatherers need Java 24 or later, shared code can only use them once every consumer runs that version.
Why this question is asked
Every large codebase has a utility class full of stream helpers that grew before gatherers existed: a batching collector here, a hand-written Spliterator there, a loop that deduplicates neighbouring events. Gatherers give these a proper home, but adding them to shared code is a design decision with costs, not just a refactoring. Interviewers ask this to see how we weigh a new language feature against the existing options and the people who consume our code. In this answer, we will recap the older tools and their limits, see what a gatherer adds with two small examples, walk through a decision process, cover testing, and finish with the Java version question.
1A Quick Recap of the Older Options
Before Java 24 there were three ways to add a custom step to stream processing, and each had a limit. A custom Collector, written with Collector.of, plugs into collect at the end of the pipeline. It can keep state, but it must consume every element and produces one result, so it cannot stop early and cannot feed later stream stages without starting a new stream.
A custom Spliterator wraps a source and can transform elements as they are pulled, which keeps laziness, but it is verbose, it has to be placed at the start of a pipeline, and getting its characteristics and splitting right takes care. A plain loop is often the clearest of all, but it gives up composition with the rest of a stream pipeline. A gatherer is the missing piece: a custom intermediate operation that keeps state, passes elements on lazily, can short-circuit, and composes with andThen.
2Two Typical Custom Gatherers
Deduplicating consecutive elements, like the Unix uniq command, is a classic. An event stream reports a sensor state each second, and we only want the changes. distinct() would remove every repeat, even later ones, so it is the wrong tool.
Notice what the second example gets for free. Because a gatherer passes elements on as it goes, limit(10) can stop the stream as soon as ten distinct users have passed, even if users is a lazily loaded stream of millions. A collector-based distinctBy would have read every user first. Both gatherers are a dozen lines, read top to bottom, and replace helpers that used to need a custom Spliterator.
3Deciding Whether a Custom Gatherer Belongs in Shared Code
The first question is whether we need anything custom at all. Gatherers.windowFixed already does batching, windowSliding does neighbour comparisons, scan does running values, fold does a mid-pipeline reduction and mapConcurrent does bounded concurrent mapping. A custom gatherer that reimplements one of these is pure cost.
The second question is whether the step is really an intermediate operation with state across elements. Stateless steps are just map or filter. A step that needs the whole input at once, like sorting by a computed rank, is a collector or a plain method. A gatherer earns its place when the step remembers something between elements and the pipeline benefits from staying lazy or short-circuiting.
The third question is whether several teams need it. A gatherer used once is often clearer as a loop in that one place. When the same pattern appears across services, a tested, documented gatherer in a shared library is better than five slightly different copies.
- Does a built-in in Gatherers already do it?YesUse the built-inNoNext question
- Does the step keep state across elements?YesNext questionNoUse map, filter or flatMap
- Must it stay lazy or stop early?YesNext questionNoA collector or plain method may be simpler
- Is it needed in more than one place?YesWrite a tested gatherer in a shared libraryNoConsider a local loop
- Do all consumers run Java 24 or later?YesShip itNoWait, or keep the older helper
5 steps in order. Play them, or step through them one at a time.
Built-ins first; a custom gatherer must earn its place and its version requirement.
4Testing a Custom Gatherer
Gatherers have edge cases that ordinary code paths do not exercise, so a shared one needs tests that target them deliberately.
The early-termination test is the important one. It proves the integrator honours the boolean from downstream.push, so the gatherer works with limit, findFirst and anyMatch on infinite or very large sources. If a gatherer is built with Gatherer.of and a combiner, so that it claims parallel support, we also run it on a parallel stream with many random inputs and compare the result with the sequential one; a combiner that merges states incorrectly only shows up there. Gatherers with a finisher need a test that the last partial state is flushed exactly once.
5The Java Version Constraint
Gatherers were a preview in Java 22 and 23 and became final in Java 24. A library compiled with --release 21 cannot reference java.util.stream.Gatherer at all; the compiler reports that the symbol does not exist. So any shared library that still supports Java 21 consumers cannot use gatherers in its public API or even internally, unless it adds a multi-release JAR with a separate Java 24 code path, which doubles the testing for little gain.
In practice, the decision about custom gatherers in shared code is often a decision about the library's minimum Java version. If the consumers are all on Java 25, the current LTS, go ahead. If not, keep the existing helper until they are, and record the plan to replace it.
6Follow-up questions
Why can a collector not short-circuit?
The Collector interface has no way to say it is done. Its accumulator returns nothing and is called for every element, and the result is only produced by the finisher after the source is exhausted. A gatherer's integrator returns a boolean, and false tells the stream to stop pulling elements.
Should every custom gatherer support parallel streams?
No. Order-dependent gatherers such as distinctConsecutive or moving averages have no meaningful parallel form, so Gatherer.ofSequential is correct and honest. Add a combiner only when the operation can genuinely merge partial states, and then test it in parallel.
Is a for loop ever better than a custom gatherer?
Often, when the logic is used in one place, has several branches, or interacts with surrounding state. A loop is easy to read and debug. Gatherers shine when the step is reusable and composes with the rest of a stream pipeline.
7Conclusion
In this answer, we saw that gatherers fill the gap between collectors, which only run at the end and cannot short-circuit, and custom Spliterators or loops, which are verbose or do not compose. We decide on a custom gatherer by checking the built-ins first, confirming the step needs state across elements and benefits from laziness, and putting it in a shared library when several teams need it. We test empty streams, early termination and, for gatherers that claim it, parallel combining. And because gatherers need Java 24 or later, adopting them in shared code is tied to the minimum Java version of its consumers.
Open with
I would reach for a custom gatherer when the step is an intermediate operation that needs state across elements, things like removing consecutive duplicates, keeping the first element per key, or batching by some custom rule, and when it matters that the pipeline stays lazy or can stop early.
Then, if they want more
Collectors cannot do that: they only run at the end, they consume every element, and they produce one result, so a collector-based distinctBy reads millions of users even if I only want ten. Custom Spliterators keep laziness but are verbose and only work at the source, and loops do not compose.
My process is: first check the built-ins in Gatherers, since windowFixed, windowSliding, scan, fold and mapConcurrent cover a lot; then confirm the step really needs state; then decide whether it is shared enough to belong in a library rather than a local loop.
If we write one, it needs tests for the empty stream, for early termination, meaning it honours the boolean from downstream.push so limit on an infinite source stops, for nulls, and, if it has a combiner and claims parallel support, a comparison of parallel and sequential results.
Finally, gatherers are final only from Java 24, so a library that still supports Java 21 consumers cannot use them, and the decision is really about the library's minimum version.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 21, Java 22, Java 25
- 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 "Before gatherers, teams wrote custom Collectors, Spliterators or loops for things like...": https://scrivio-interview-library.netlify.app/java/streams/bb3f8de19d/