如何使用 java 8 流和 lambda 对 groupingBy 结果进行平面映射
how to use java 8 stream and lambda to flatMap a groupingBy result
我有一个包含其他对象列表的对象,我想 return 由容器的某些 属性 映射的包含对象的平面图。是否可以仅使用流和 lambda 表达式?
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>>
您可以尝试类似的方法:
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;
}))));
在 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())));
我有一个包含其他对象列表的对象,我想 return 由容器的某些 属性 映射的包含对象的平面图。是否可以仅使用流和 lambda 表达式?
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>>
您可以尝试类似的方法:
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;
}))));
在 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())));