Quick revision
The short answer
partitioningBy splits elements into a Map<Boolean, ...> by a predicate, and both the true and false keys are always present. collectingAndThen applies a finisher to another collector's result, for example to make it unmodifiable or to unwrap an Optional. Downstream collectors such as counting, summingInt and maxBy aggregate each group, and flatMapping and filtering (Java 9) apply flatMap or filter inside a group. teeing (Java 12) feeds every element to two collectors and merges their results, for example computing the minimum and maximum in one pass.
What to remember
In this answer, we walked through the less familiar collectors. partitioningBy splits elements by a predicate into a map with both true and false keys always present. counting, summingInt and maxBy aggregate each group, and collectingAndThen finishes a collector's result, here unwrapping maxBy's Optional or making a list unmodifiable. flatMapping and filtering, from Java 9, flatten or filter inside each group while keeping every key. teeing, from Java 12, feeds two collectors in one pass and merges their results, as in computing the minimum and maximum salary together.
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.
partitioningBy splits elements into a Map<Boolean, ...> by a predicate, and both the true and false keys are always present. collectingAndThen applies a finisher to another collector's result, for example to make it unmodifiable or to unwrap an Optional. Downstream collectors such as counting, summingInt and maxBy aggregate each group, and flatMapping and filtering (Java 9) apply flatMap or filter inside a group. teeing (Java 12) feeds every element to two collectors and merges their results, for example computing the minimum and maximum in one pass.
Why this question is asked
Collectors are the vocabulary of stream aggregation, and most developers know only toList, toMap and groupingBy. The rest of the vocabulary turns many hand-written loops with maps of counters into one readable expression, and it matters at a mid-level interview because it shows we can compose small pieces rather than write everything from scratch. In this answer, we will recap what a downstream collector is, then go through partitioningBy, the aggregating downstream collectors, collectingAndThen, flatMapping and filtering, and finally teeing with a use case, all on the same sample data.
1A Quick Recap of Downstream Collectors
A collector describes how to accumulate stream elements into a result. Some collectors, such as groupingBy and partitioningBy, split elements into groups and then need a second collector to say what to do with each group; that second one is called the downstream collector. Because collectors compose, a downstream collector can itself take another downstream collector, which is how we build expressions such as group by department, then within each group map to names and collect to a set.
We will use this data in every example.
2PartitioningBy: Two Groups, Always Both Keys
partitioningBy(predicate) is a special case of grouping where the key is the boolean result of a predicate. Its result is a Map<Boolean, ...> with exactly two entries, and both keys are always present, even when one group is empty. That is the important difference from groupingBy(e -> e.salary() >= 100), which would create only the keys that occur.
Notice that code reading senior.get(true) never has to handle a missing key, which removes a common source of NullPointerException.
3Aggregating Each Group: counting, summingInt and maxBy
The aggregating collectors turn a group into a single value. counting() counts the elements and returns a Long, summingInt sums an int property, and maxBy and minBy return the largest or smallest element by a comparator, wrapped in an Optional because a stream may in general be empty.
The Optional in the last result is awkward, because groupingBy never creates an empty group. That is exactly the kind of clean-up collectingAndThen is for.
4CollectingAndThen: Finishing Another Collector's Result
collectingAndThen(collector, finisher) runs a collector and then applies a function to its result. Two common uses are unwrapping or transforming a result, and making a collection unmodifiable.
Notice that the finisher runs once per group when used as a downstream collector, so each department's Optional is unwrapped separately.
5FlatMapping and filtering Inside a Group
Java 9 added flatMapping and filtering as downstream collectors. flatMapping turns each element into a stream and collects all of those elements, which is how we gather the skills of everyone in a department. filtering applies a predicate inside each group.
Why not just call filter before groupingBy? Because then HR and OPS would disappear from the result entirely. filtering keeps every department as a key and filters only inside the groups, which is what a report showing every department needs.
6Teeing: Two Collectors in One Pass
Collectors.teeing(first, second, merger), added in Java 12, sends every element to two collectors at once and then combines their two results with a merger function. Its name comes from a T-shaped pipe fitting that splits one flow into two. A classic use is computing the minimum and maximum, or a sum and a count, in a single pass instead of streaming the data twice.
| Collector | Result per group | Since |
|---|---|---|
partitioningBy(pred, ds) | Map<Boolean, ...> with both keys | Java 8 |
counting() | Long | Java 8 |
summingInt(f) | Integer sum | Java 8 |
maxBy(cmp), minBy(cmp) | Optional of an element | Java 8 |
collectingAndThen(c, f) | f applied to the result of c | Java 8 |
flatMapping(f, ds), filtering(p, ds) | flatMap or filter inside a group | Java 9 |
teeing(c1, c2, merger) | merger(result1, result2) | Java 12 |
This matters most when the source can be read only once or is expensive to read, such as rows streamed from a database. teeing can also be used as a downstream collector, so we could compute a salary range per department the same way.
7Follow-up questions
Is partitioningBy faster than groupingBy with a boolean key?
Slightly, because it uses a small dedicated two-entry map instead of a HashMap, but the main reason to use it is the guarantee that both keys exist. That makes the calling code simpler and safer.
Why does counting() return Long rather than Integer?
Because a stream can have more elements than an int can count, and the collector is defined in terms of long arithmetic. If we need an int, we can use collectingAndThen(counting(), Long::intValue) or summingInt(e -> 1).
When would teeing be the wrong tool?
When the two calculations are unrelated or the merger becomes complex, two separate, clearly named pipelines over a list are easier to read. teeing pays off when a single pass matters, or when the two results naturally form one value such as a range or an average.
8Conclusion
In this answer, we walked through the less familiar collectors. partitioningBy splits elements by a predicate into a map with both true and false keys always present. counting, summingInt and maxBy aggregate each group, and collectingAndThen finishes a collector's result, here unwrapping maxBy's Optional or making a list unmodifiable. flatMapping and filtering, from Java 9, flatten or filter inside each group while keeping every key. teeing, from Java 12, feeds two collectors in one pass and merges their results, as in computing the minimum and maximum salary together.
Open with
partitioningBy takes a predicate and gives a Map from Boolean to whatever the downstream collector produces, and both keys, true and false, are always present even if a group is empty, which is the difference from groupingBy with a boolean key.
Then, if they want more
Downstream collectors are the second argument of groupingBy or partitioningBy and say what to do with each group: counting gives a Long count, summingInt sums a property, maxBy and minBy give the largest or smallest element as an Optional. collectingAndThen wraps another collector and applies a finisher to its result.
I use it to unwrap maxBy's Optional, since groups are never empty, or to make a collected list unmodifiable.
Java 9 added flatMapping and filtering as downstream collectors: flatMapping lets me collect, say, all skills of the people in each department into a set, and filtering filters inside each group, so departments with no matches still appear as keys with an empty list, unlike calling filter before grouping. teeing, from Java 12, sends every element to two collectors at once and merges the two results with a function.
My standard use case is computing the minimum and maximum salary, or a sum and a count, in a single pass, which matters when the data comes from a source that's expensive or impossible to read twice, like rows streamed from a database.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 12, Java 9
- 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 "Explain partitioningBy, teeing (Java 12), collectingAndThen, and downstream collectors...": https://scrivio-interview-library.netlify.app/java/streams/766797c9fa/