如何使用java 8 stream和lambda来flatMap一个groupingBy结果

War*_*eto 4 java lambda java-8 java-stream collectors

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

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

public class Product{
   String name;
   String value;
}
Run Code Online (Sandbox Code Playgroud)

让我们来处理一系列操作:

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

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

结果将是善意的

Map<String, List<List<Product>>> 
Run Code Online (Sandbox Code Playgroud)

但我想像它一样扁平化

Map<String, List<Product>>
Run Code Online (Sandbox Code Playgroud)

Tag*_*eev 9

在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]));
}
Run Code Online (Sandbox Code Playgroud)

您可以将它添加到您的项目并使用如下:

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


Den*_*ker 7

你可以尝试类似的东西:

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;
        }))));
Run Code Online (Sandbox Code Playgroud)

  • 修改函数的传入值时,请勿使用`reduce`或`Collectors.reducing`!您可以使用`Collector.of(ArrayList :: new,List :: addAll,(x,y) - > {x.addAll(y); return x;})`.作为遵守合同的奖励,这可以并行工作. (3认同)