问题描述
在什么情况下,在Java 8流中调用'reduce'的第三个参数?
下面的代码尝试遍历字符串列表并合并代码每个点的第一个字符的点值。最终lambda返回的值似乎永远不会被使用,如果插入println,它似乎永远不会被调用。文档将其描述为组合器,但我无法找到更多详细信息...
int result =
data (0,(total,s)→> total + s.codePointAt(0),(a,b) - > 1000000);
您是在谈论 $ b
我假设它的目的是允许并行计算,所以我的猜测仅当并行执行缩减时才使用它。如果按顺序执行,则不需要使用组合器
。我不知道这是肯定的 - 我只是猜测基于文档评论[...]不限制顺序执行以及评论中的其他许多提及并行执行的内容。
Under what circumstances is the third argument to 'reduce' called in Java 8 streams?
The code below attempts to traverse a list of strings and add up the code point values of the first character of each. The value returned by the final lambda never seems to be used and, if you insert a println, it never seems to be invoked. The documentation describes it as a 'combiner' but I cant find more detail...
int result =
data.stream().reduce(0, (total,s) -> total + s.codePointAt(0), (a,b) -> 1000000);
Are you talking about this function?
I assume its purpose is to allow parallel computation, and so my guess is that it's only used if the reduction is performed in parallel. If it's performed sequentially, there's no need to use combiner
. I do not know this for sure -- I'm just guessing based on the doc comment "[...] is not constrained to execute sequentially" and the many other mentions of "parallel execution" in the comments.
这篇关于Java 8函数式编程中“减少”函数的第三个参数的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!