Java 8嵌套(多级)分组依据

我下面有几节课


class Pojo {

    List<Item> items;

}


class Item {

    T key1;

    List<SubItem> subItems;

}


class SubItem {

    V key2;

    Object otherAttribute1;

}

我想基于key1每个聚合来聚合项目,对于子项目,应按key2以下方式聚合:


Map<T, Map<V, List<Subitem>>

Java 8 Collectors.groupingBy嵌套怎么可能?


我正在尝试某事,并在


pojo.getItems()

    .stream()

    .collect(

        Collectors.groupingBy(Item::getKey1, /* How to group by here SubItem::getKey2*/)

    );

注意:这与级联不同,级联groupingBy基于此处讨论的同一对象中的字段进行多级聚合


qq_遁去的一_1
浏览 762回答 3
3回答

翻过高山走不出你

您不能通过多个键对单个项目进行分组,除非您接受该项目可能会出现在多个组中。在这种情况下,您想要执行一种flatMap操作。实现此目的的一种方法是Stream.flatMap与临时对一起使用,以容纳和的组合,Item然后SubItem再进行收集。由于不存在标准对类型,因此典型的解决方案是Map.Entry:Map<T, Map<V, List<SubItem>>> result = pojo.getItems().stream()&nbsp; &nbsp; .flatMap(item -> item.subItems.stream()&nbsp; &nbsp; &nbsp; &nbsp; .map(sub -> new AbstractMap.SimpleImmutableEntry<>(item, sub)))&nbsp; &nbsp; .collect(Collectors.groupingBy(e -> e.getKey().getKey1(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.mapping(Map.Entry::getValue,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.groupingBy(SubItem::getKey2))));不需要这些临时对象的替代方法是flatMap直接在收集器中执行操作,但是不幸的flatMapping是,直到Java 9才存在。这样,解决方案看起来像Map<T, Map<V, List<SubItem>>> result = pojo.getItems().stream()&nbsp; &nbsp; .collect(Collectors.groupingBy(Item::getKey1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.flatMapping(item -> item.getSubItems().stream(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.groupingBy(SubItem::getKey2))));如果我们不想等待Java 9,可以在代码库中添加一个类似的收集器,因为实现起来并不难:static <T,U,A,R> Collector<T,?,R> flatMapping(&nbsp; &nbsp; Function<? super T,? extends Stream<? extends U>> mapper,&nbsp; &nbsp; Collector<? super U,A,R> downstream) {&nbsp; &nbsp; BiConsumer<A, ? super U> acc = downstream.accumulator();&nbsp; &nbsp; return Collector.of(downstream.supplier(),&nbsp; &nbsp; &nbsp; &nbsp; (a, t) -> { try(Stream<? extends U> s=mapper.apply(t)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(s!=null) s.forEachOrdered(u -> acc.accept(a, u));&nbsp; &nbsp; &nbsp; &nbsp; }},&nbsp; &nbsp; &nbsp; &nbsp; downstream.combiner(), downstream.finisher(),&nbsp; &nbsp; &nbsp; &nbsp; downstream.characteristics().toArray(new Collector.Characteristics[0]));}

桃花长相依

如果将分组为Map<T, List<…>>,则T键是唯一的,但是当将多个分组为时Map<T, Map<V, List<…>>>,V键仅在其子图中是唯一的。但是,根据您的输入数据结构和实际目标,这可能(可能)不是问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java