how to use java 8 stream and lambda to flatMap a groupingBy result

后端 未结 2 1894
陌清茗
陌清茗 2020-12-17 03:42

I have a object with contains a list of other objects and I want to return a flatmap of the contained objects mapped by some property of the container. Any one if is it poss

2条回答
  •  自闭症患者
    2020-12-17 04:27

    You could try something like:

    Map> res = operations.parallelStream().filter(s -> s.getTotal() > 10)
        .collect(groupingBy(Selling::getClientName, mapping(Selling::getProducts,
            Collector.of(ArrayList::new, List::addAll, (x, y) -> {
                x.addAll(y);
                return x;
            }))));
    

提交回复
热议问题