Java 8 Stream API - Java 9 Collectors.flatMapping rewritten in Java 8(Java 8 Stream API - Java 9 Collectors.flatMapping 用 Java 8 重写)
问题描述
自从 java-9 调用 Collectors.flatMapping
作为分组或分区的下游发生.例如(示例取自 here):
列表<列表<整数>>列表 = Arrays.asList(Arrays.asList(1, 2, 3, 4, 5, 6),Arrays.asList(7, 8, 9, 10));映射<整数、列表<整数>>地图 =list.stream().collect(Collectors.groupingBy(收藏::尺寸,Collectors.flatMapping(l ->l.stream().filter(i -> i % 2 == 0),Collectors.toList())));
<块引用>
{4=[8, 10], 6=[2, 4, 6]}
这是一种相当优雅的方式,只使用 3 个收集器.我需要在 java-8 尚不支持的地方.我尝试使用 6 收集器,这是一种相当广泛的用法,但我无法找到使用较少它们的方法:
映射<整数、列表<整数>>地图 = list.stream().collect(Collectors.groupingBy(收藏::尺寸,Collectors.collectingAndThen(收藏家.映射(l ->l.stream().filter(i -> i % 2 == 0).collect(Collectors.toList()),Collectors.toList()),我->i.stream().flatMap(j -> j.stream()).collect(Collectors.toList()))));
有没有一个 更短 更好的方法,只使用 java-8?
对于 this 特殊情况,我想这将是一个更简单的版本:
映射<整数、列表<整数>>地图=list.stream().collect(Collectors.toMap(收藏::尺寸,x ->x.stream().filter(y -> y % 2 == 0).collect(Collectors.toList())));
如果需要合并(两个大小相同的集合),我会添加一个非常简单的 merge
函数:
映射<整数、列表<整数>>地图=list.stream().collect(Collectors.toMap(收藏::尺寸,x ->x.stream().filter(y -> y % 2 == 0).collect(Collectors.toCollection(ArrayList::new)),(左,右)->{left.addAll(right);向左返回;}));
否则,我同意 Michael 在此评论中的观点,这并不难向后移植到 java-8.
I got in touch with a new feature since java-9 called Collectors.flatMapping
that takes place as a downstream of grouping or partitioning. Such as (example taken from here):
List<List<Integer>> list = Arrays.asList(
Arrays.asList(1, 2, 3, 4, 5, 6),
Arrays.asList(7, 8, 9, 10));
Map<Integer, List<Integer>> map =list.stream()
.collect(Collectors.groupingBy(
Collection::size,
Collectors.flatMapping(
l -> l.stream().filter(i -> i % 2 == 0),
Collectors.toList())));
{4=[8, 10], 6=[2, 4, 6]}
This is a fairly elegant way using just 3 collectors. I need to rewrite the collector in java-8 where is not yet supported. My attempt use 6 Collectors that is quite an extensive usage and I am not able to figure out a way using less of them:
Map<Integer, List<Integer>> map = list.stream()
.collect(Collectors.groupingBy(
Collection::size,
Collectors.collectingAndThen(
Collectors.mapping(
l -> l.stream().filter(i -> i % 2 == 0).collect(Collectors.toList()),
Collectors.toList()),
i -> i.stream().flatMap(j -> j.stream()).collect(Collectors.toList()))));
Is there a shorter better way using solely java-8?
For just this particular case, I guess this would be a simpler version:
Map<Integer, List<Integer>> map =
list.stream()
.collect(Collectors.toMap(
Collection::size,
x -> x.stream().filter(y -> y % 2 == 0).collect(Collectors.toList())
));
If there would be merging involved (two collections that would have the same size), I would add a merge
function that is pretty trivial:
Map<Integer, List<Integer>> map =
list.stream()
.collect(Collectors.toMap(
Collection::size,
x -> x.stream().filter(y -> y % 2 == 0).collect(Collectors.toCollection(ArrayList::new)),
(left, right) -> {
left.addAll(right);
return left;
}
));
Otherwise, I agree with Michael in this comment, this is not that hard to back-port to java-8.
这篇关于Java 8 Stream API - Java 9 Collectors.flatMapping 用 Java 8 重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java 8 Stream API - Java 9 Collectors.flatMapping 用 Java 8 重写
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01