【问题标题】:how to use java 8 stream and lambda to flatMap a groupingBy result如何使用 java 8 流和 lambda 来 flatMap 一个 groupingBy 结果
【发布时间】:2015-11-22 00:40:11
【问题描述】:

我有一个包含其他对象列表的对象,我想返回由容器的某些属性映射的包含对象的平面图。是否可以仅使用流和 lambdas?

public class Selling{
   String clientName;
   double total;
   List<Product> products;
}

public class Product{
   String name;
   String value;
}

让我们假设一个操作列表:

List<Selling> operations = new ArrayList<>();

operations.stream()
     .filter(s -> s.getTotal > 10)
     .collect(groupingBy(Selling::getClientName, mapping(Selling::getProducts, toList());

结果会很好

Map<String, List<List<Product>>> 

但我想把它弄平

Map<String, List<Product>>

【问题讨论】:

    标签: java lambda java-8 java-stream collectors


    【解决方案1】:

    你可以试试这样的:

    Map<String, List<Product>> 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;
            }))));
    

    【讨论】:

    • 修改函数传入值时不要使用reduceCollectors.reducing!您可以改用Collector.of(ArrayList::new, List::addAll, (x, y) -&gt; { x.addAll(y); return x; })。作为遵守合同的奖励,这可以并行工作。
    • 好点,根据您的评论更新了代码,以便在与 parallelStream 一起使用时保存。
    【解决方案2】:

    在 JDK9 中有一个新的标准收集器,称为 flatMapping,可以通过以下方式实现:

    public 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> downstreamAccumulator = downstream.accumulator();
        return Collector.of(downstream.supplier(),
                (r, t) -> {
                    try (Stream<? extends U> result = mapper.apply(t)) {
                        if (result != null)
                            result.sequential().forEach(u -> downstreamAccumulator.accept(r, u));
                    }
                },
                downstream.combiner(), downstream.finisher(),
                downstream.characteristics().toArray(new Collector.Characteristics[0]));
    }
    

    您可以将它添加到您的项目中并像这样使用:

    operations.stream()
       .filter(s -> s.getTotal() > 10)
       .collect(groupingBy(Selling::getClientName, 
                  flatMapping(s -> s.getProducts().stream(), toList())));
    

    【讨论】:

    • 它是否包含在某些 JSR 中?
    • @Madis,没有。简单的 API 更改(例如添加一种新方法)通常会绕过 JSR/JEP 流程。当然,它在 OpenJDK 错误跟踪器中有 an issue(每个功能都必须有问题 ID)。
    猜你喜欢
    • 2018-06-02
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多