Home > Java > javaTutorial > body text

How to Group Java 8 Nested Objects by Multiple Keys Using Collectors?

DDD
Release: 2024-10-24 08:38:02
Original
227 people have browsed it

How to Group Java 8 Nested Objects by Multiple Keys Using Collectors?

Java 8 Multi-Level Grouping with Nested Collectors

Multi-level grouping, also known as nested grouping, involves aggregating data by multiple keys across different levels of a nested object hierarchy. In Java 8, you can achieve this with the help of nested Collectors.

Consider a scenario where you have classes like Pojo, Item, and SubItem, where each Pojo has a list of Items, and each Item has a list of SubItems. The goal is to group the Items by their key1 field, and for each group, further aggregate the SubItems by their key2 field.

To perform this nested grouping, you can use Collectors.groupingBy with a nested structure:

<code class="java">pojo.getItems()
    .stream()
    .collect(
        Collectors.groupingBy(Item::getKey1, **// How to group by SubItem::getKey2 here**));</code>
Copy after login

The question lies in the second parameter of Collectors.groupingBy - how to group the SubItems by key2 within each key1 group. The solution cannot utilize cascaded groupingBy, as it only groups by fields within the same object.

The answer involves using Stream.flatMap to create a stream of key-value pairs, where the key is Item.key1 and the value is SubItem.key2. This stream is then grouped using Collectors.groupingBy to achieve the desired nested grouping:

<code class="java">Map<T, Map<V, List<SubItem>>> result = pojo.getItems().stream()
    .flatMap(item -> item.getSubItems().stream()
        .map(sub -> new AbstractMap.SimpleImmutableEntry<>(item.getKey1(), sub)))
    .collect(Collectors.groupingBy(AbstractMap.SimpleImmutableEntry::getKey,
                Collectors.mapping(Map.Entry::getValue,
                    Collectors.groupingBy(SubItem::getKey2))));</code>
Copy after login

Alternatively, with Java 9's Collectors.flatMapping, the solution simplifies to:

<code class="java">Map<T, Map<V, List<SubItem>>> result = pojo.getItems().stream()
    .collect(Collectors.groupingBy(Item::getKey1,
                Collectors.flatMapping(item -> item.getSubItems().stream(),
                    Collectors.groupingBy(SubItem::getKey2))));</code>
Copy after login

The above is the detailed content of How to Group Java 8 Nested Objects by Multiple Keys Using Collectors?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!