In Stream reduce method, must the identity always be 0 for sum and 1 for multiplication?(在Stream reduce方法中,求和必须始终为0,乘法必须始终为1吗?)
问题描述
我继续学习 java 8.
I proceed with java 8 learning.
我发现了一个有趣的行为:
I have found an interesting behavior:
让我们看看代码示例:
// identity value and accumulator and combiner
Integer summaryAge = Person.getPersons().stream()
//.parallel() //will return surprising result
.reduce(1,
(intermediateResult, p) -> intermediateResult + p.age,
(ir1, ir2) -> ir1 + ir2);
System.out.println(summaryAge);
和模型类:
public class Person {
String name;
Integer age;
///...
public static Collection<Person> getPersons() {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Vasya", 12));
persons.add(new Person("Petya", 32));
persons.add(new Person("Serj", 10));
persons.add(new Person("Onotole", 18));
return persons;
}
}
12+32+10+18 = 72
.对于顺序流,此代码始终返回 73
,即 72 + 1
,但对于并行,它始终返回 76
,即 72 +4*1
(4 等于流元素数).
12+32+10+18 = 72
. For sequential stream, this code always returns 73
which is 72 + 1
but for parallel, it always returns 76
which is 72 + 4*1
(4 is equal to stream elements count).
当我看到这个结果时,我认为并行流和顺序流返回不同的结果很奇怪.
When I saw this result I thought that it is strange that parallel stream and sequential streams return different results.
我是不是在什么地方违约了?
Am I broke contract somewhere?
对我来说,73 是预期结果,但 76 不是.
for me, 73 is expected result but 76 is not.
推荐答案
identity值是一个值,使得x op identity = x
.这不是 Java Stream
所独有的概念,例如参见 on Wikipedia.
The identity value is a value, such that x op identity = x
. This is a concept which is not unique to Java Stream
s, see for example on Wikipedia.
它列出了一些标识元素的例子,其中一些可以直接用Java代码表示,例如
It lists some examples of identity elements, some of them can be directly expressed in Java code, e.g.
reduce("", String::concat)
reduce(true, (a,b) -> a&&b)
reduce(false, (a,b) -> a||b)
reduce(Collections.emptySet(),(a,b)->{设置<X>s=新哈希集<>(a);s.addAll(b);返回 s;})
reduce(Double.POSITIVE_INFINITY, Math::min)
reduce(Double.NEGATIVE_INFINITY, Math::max)
应该清楚的是,任意x
的表达式x + y == x
只能在y==0
时满足,因此 0
是加法的标识元素.同样,1
是乘法的标识元素.
It should be clear that the expression x + y == x
for arbitrary x
can only be fulfilled when y==0
, thus 0
is the identity element for the addition. Similarly, 1
is the identity element for the multiplication.
更复杂的例子是
减少谓词流
Reducing a stream of predicates
reduce(x->true, Predicate::and)
reduce(x->false, Predicate::or)
减少函数流
Reducing a stream of functions
reduce(Function.identity(), Function::andThen)
这篇关于在Stream reduce方法中,求和必须始终为0,乘法必须始终为1吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在Stream reduce方法中,求和必须始终为0,乘法必须始终为1吗?
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01