首页  >  文章  >  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