【发布时间】:2018-06-20 14:50:01
【问题描述】:
我在具有相同 lambda 表达式的同一个数组上使用 reduce 与 stream 和 parallelStream,我希望得到相同的结果,但输出不同。但是,结果不同,我不知道为什么。
代码:
System.out.println("Reduce me...");
Integer[] arrx = {1,2,3,4};
// Reducing the Array
Arrays
.stream(arrx)
.reduce((s1, s2) -> (int)Math.pow(s1,2) + (int)Math.pow(s2,2))
.ifPresent(System.out::println);
Arrays.asList(arrx)
.parallelStream()
.reduce((s1, s2) -> (int)Math.pow(s1,2) + (int)Math.pow(s2,2))
.ifPresent(System.out::println);
输出:
1172
650
【问题讨论】:
-
您研究过
stream和parallelStream之间的区别吗?如果您已经知道这会导致不同的结果... -
@Gatusko 这可能不是完全相同的副本,但从这个问题中肯定可以学到很多东西。感谢您的链接。