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

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

问题描述

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

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

  int result = 
data (0,(total,s)→> total + s.codePointAt(0),(a,b) - > 1000000);


解决方案

您是在谈论 $ b


 减少< U> U减少(U标识,
BiFunction 累加器,
BinaryOperator组合器)

使用提供的身份,累计和
组合函数执行此流元素的缩减。这相当于:


  U result = identity; 
for(T element:this stream)
result = accumulator.apply(result,element)
返回结果;

但不限制顺序执行。身份值必须是组合函数的身份。这
表示对于所有你,组合者(身份,u)等于u。
此外,组合器功能必须与
累加器功能兼容;对于所有u和t,必须满足以下条件:

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

这是一个终端操作。 / p>

API注意:使用这种形式的许多缩减可以简单地通过映射和缩小操作的显式组合来代表更多

累加器函数作为一个融合映射器和累加器,可以
有时比单独的映射和缩减效率更高,例如
,因为知道先前减少的值可以避免一些
计算。类型参数:U - 结果的类型参数:
identity - 组合函数累加器的标识值 -
一个关联的,无干扰的无状态函数,用于将
一个附加元素并入一个结果组合器 - 一个联合的
无干扰无状态函数,用于组合两个值,其中
必须与累加器函数兼容返回:减少的结果
另请参见:reduce BinaryOperator),reduce(Object,
BinaryOperator)

我假设它的目的是允许并行计算,所以我的猜测仅当并行执行缩减时才使用它。如果按顺序执行,则不需要使用组合器。我不知道这是肯定的 - 我只是猜测基于文档评论[...]不限制顺序执行以及评论中的其他许多提及并行执行的内容。


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

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