Java Reference
In-Depth Information
extended to any number of levels, and an n -level grouping has as a result an n -level Map
modeling an n -level tree structure.
Figure 6.5 shows how this structure is also equivalent to an n -dimensional table, highlighting
the classification purpose of the grouping operation.
Figure 6.5. Equivalence between n -level nested map and
n -dimensional classification table
In general, it helps to think that groupingBy works in terms of “buckets.” The first groupingBy
creates a bucket for each key. You then collect the elements in each bucket with the downstream
collector and so on to achieve n -level groupings!
6.3.2. Collecting data in subgroups
In the previous section, you saw that it's possible to pass a second groupingBy collector to the
outer one to achieve a multilevel grouping. But more generally, the second collector passed to
the first groupingBy can be any type of collector, not just another groupingBy. For instance, it's
possible to count the number of Dishes in the menu for each type, by passing the counting
collector as a second argument to the groupingBy collector:
 
Search WWH ::




Custom Search