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):
<块引用>
{4=[8, 10], 6=[2, 4, 6]}
这是一种相当优雅的方式,只使用 3 个收集器.我需要在 java-8 尚不支持的地方.我尝试使用 6 收集器,这是一种相当广泛的用法,但我无法找到使用较少它们的方法:
有没有一个 更短 更好的方法,只使用 java-8?
对于 this 特殊情况,我想这将是一个更简单的版本:
如果需要合并(两个大小相同的集合),我会添加一个非常简单的 merge
函数:
否则,我同意 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):
{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:
Is there a shorter better way using solely java-8?
For just this particular case, I guess this would be a simpler version:
If there would be merging involved (two collections that would have the same size), I would add a merge
function that is pretty trivial:
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 重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!