首頁  >  文章  >  Java  >  如何在Java 8中執行嵌套的GroupBy操作以進行複雜的資料聚合?

如何在Java 8中執行嵌套的GroupBy操作以進行複雜的資料聚合?

Patricia Arquette
Patricia Arquette原創
2024-10-24 08:50:02461瀏覽

How to Perform Nested GroupBy Operations in Java 8 for Complex Data Aggregation?

Java 8 中的巢狀(多層)GroupBy

Java 8 中的多層分組允許基於資料物件的複雜聚合個字段。在您的範例中,您有一組類別 Pojo、Item 和 SubItem,並且您希望根據 key1 對項目進行分組,然後根據 key2 對該群組內的子項目進行分組。

要實現這一點,我們不能簡單地使用嵌套的 Collectors.groupingBy 調用,因為這不允許按來自不同物件的多個鍵進行分組。相反,我們採用 flatMap 和分組收集器的組合:

<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.groupingBy(Map.Entry::getValue, Collectors.toList())));</code>

在這個方法中,我們首先使用 flatMap 建立一個包含每個 Item 和對應 SubItem 的 key1 的對流。然後,我們套用 Collectors.groupingBy 兩次:一次按 key1 對對進行分組,再次按 key2 對子項目進行分組。

另一個解決方案是定義一個自訂收集器,它提供類似Java 9 的flatMapping 操作Collectors.flatMapping:

<code class="java">static <T,U,A,R> Collector<T,?,R> flatMapping(
    Function<? super T,? extends Stream<? extends U>> mapper,
    Collector<? super U,A,R> downstream) {

    BiConsumer<A, ? super U> acc = downstream.accumulator();
    return Collector.of(downstream.supplier(),
        (a, t) -> { try(Stream<? extends U> s=mapper.apply(t)) {
            if(s!=null) s.forEachOrdered(u -> acc.accept(a, u));
        }},
        downstream.combiner(), downstream.finisher(),
        downstream.characteristics().toArray(new Collector.Characteristics[0]));
}</code>

使用這個自訂收集器,可以簡化分組運算:

<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>

以上是如何在Java 8中執行嵌套的GroupBy操作以進行複雜的資料聚合?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn