SteelReed

Search

Search posts, questions…

Java CoreQuestion 34 / 43

Middle

How do Collectors.groupingBy and partitioningBy work? What is a downstream collector?

Short answer

groupingBy splits elements into groups, like GROUP BY in SQL. partitioningBy splits in two by a condition and always has both true and false groups.

groupingBy(status)downstream counting()downstream mapping(...)partitioningBy(isPaid)orders.stream()status → List of Orderstatus → Longstatus → Set of namesalways keys true, false

3 points to say:

  1. The default result is Map<Key, List<T>>.
  2. The second argument decides what each group becomes, like counting().
  3. An empty group still has its key in partitioningBy.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.