Java 8 函数式编程中“reduce"函数的第三个参数的目的 [英] Purpose of third argument to 'reduce' function in Java 8 functional programming

查看:14
本文介绍了Java 8 函数式编程中“reduce"函数的第三个参数的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下在 Java 8 流中调用reduce"的第三个参数?

下面的代码尝试遍历字符串列表并将每个字符串的第一个字符的代码点值相加.最终 lambda 返回的值似乎从未被使用过,如果您插入 println,它似乎从未被调用过.文档将其描述为组合器",但我找不到更多详细信息...

int 结果 =data.stream().reduce(0, (total,s) -> total + s.codePointAt(0), (a,b) -> 1000000);

解决方案

你在谈论 这个函数?

<块引用>

reduce U 减少(U 身份,BiFunction累加器,BinaryOperator<U>合路器)

使用提供的标识、累积和结合功能.这相当于:

 U 结果 = 身份;对于(T 元素:此流)结果 = accumulator.apply(result, element)返回结果;

但不限于顺序执行.标识值必须是组合器功能的标识.这意味着对于所有 u,combiner(identity, u) 等于 u.此外,组合器功能必须与蓄能器功能;对于所有 u 和 t,以下必须成立:

 combiner.apply(u, accumulator.apply(identity, t)) ==accumulator.apply(u, t)

这是一个终端操作.

API 注意:使用这种形式的许多减少可以表示更多简单地通过 map 和 reduce 操作的显式组合.这累加器函数充当融合映射器和累加器,它可以有时比单独的映射和减少更有效,例如因为当知道先前减少的值可以让您避免一些计算.类型参数: U - 结果的类型参数:identity - 组合器函数累加器的标识值 -一个关联的、无干扰的、无状态的函数,用于合并结果组合器中的附加元素 - 关联,用于组合两个值的无干扰、无状态函数,其中必须与累加器函数兼容返回:结果减少的另见:reduce(BinaryOperator), reduce(Object,二元运算符)

我假设它的目的是允许并行计算,所以我的猜测是只有在并行执行减少时才使用它.如果按顺序执行,则无需使用combiner.我不确定这一点——我只是根据文档评论[...] 不受限制地执行顺序"以及评论中提到的并行执行"的许多其他内容进行猜测.

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?

reduce <U> U reduce(U identity,
             BiFunction<U,? super T,U> accumulator,
             BinaryOperator<U> combiner) 

Performs a reduction on the elements of this stream, using the provided identity, accumulation and combining functions. This is equivalent to:

 U result = identity;
 for (T element : this stream)
     result = accumulator.apply(result, element)
 return result;   

but is not constrained to execute sequentially. The identity value must be an identity for the combiner function. This means that for all u, combiner(identity, u) is equal to u. Additionally, the combiner function must be compatible with the accumulator function; for all u and t, the following must hold:

 combiner.apply(u, accumulator.apply(identity, t)) == 
     accumulator.apply(u, t)   

This is a terminal operation.

API Note: Many reductions using this form can be represented more simply by an explicit combination of map and reduce operations. The accumulator function acts as a fused mapper and accumulator, which can sometimes be more efficient than separate mapping and reduction, such as when knowing the previously reduced value allows you to avoid some computation. Type Parameters: U - The type of the result Parameters: identity - the identity value for the combiner function accumulator - an associative, non-interfering, stateless function for incorporating an additional element into a result combiner - an associative, non-interfering, stateless function for combining two values, which must be compatible with the accumulator function Returns: the result of the reduction See Also: reduce(BinaryOperator), reduce(Object, BinaryOperator)

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 函数式编程中“reduce"函数的第三个参数的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆