了解Java Stream Reduce-并行vs系列 [英] Understanding Java Stream reduce - parallel vs series

查看:60
本文介绍了了解Java Stream Reduce-并行vs系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java代码,该代码使用lambda和流来查找整数列表的平均值.它使用Stream.reduce(...)查找整数之和,然后计算平均值.即使删除了parallel()调用,代码也可以正常工作.有人可以解释为什么吗?代码后还有更多问题.

I have Java code which uses lambdas and streams to find the average of a list of integers. It uses Stream.reduce(...) to find the sum of integers and then calculates average. The code works correctly even when I remove the parallel() call. Can someone explain why ? More questions after the code.

import java.util.Arrays;
import java.util.List;

public class Temp {
    public static void main(String [] args){
        //For example, consider a list of a *series* of numbers in increasing order.
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
        int n = numbers.get(numbers.size()-1);//n = 6 is the number of numbers in list.

        double expectedSum = (n * (n + 1))/2;//By mathematical formula for increasing series.

        Double sum = numbers
                //Take a stream of integers.
                .stream()
                //Split the stream into mini streams and calculate sum of each of mini stream.
                .parallel()
                //Reduce all the numbers in a stream to their sum.
                .reduce(
                        //start with a partial sum of 0.
                0.0,
                //For a stream, calculate the sum of all the numbers.
                (partialSum, nextNumber) -> partialSum + (double) nextNumber,
                //Add the sums of each mini stream.
                (onePartialSum, anotherPartialSum) -> (onePartialSum + anotherPartialSum)
        );

        System.out.println("Sum : expected value = " + expectedSum + ", actual value = " + sum);

        double expectedAverage = expectedSum/numbers.size();
        double average = sum/numbers.size();

        System.out.println("Average : expected value = " + expectedAverage + ", actual value = " + average);

    }
}

输出:

Sum : expected value = 21.0, actual value = 21.0
Average : expected value = 3.5, actual value = 3.5

请考虑以下代码,该函数是 BinaryOperator< U>组合器:

Consider the code below for the function which is a BinaryOperator<U> combiner :

(onePartialSum, anotherPartialSum) -> (onePartialSum + anotherPartialSum)

这条线实际上如何并行和不并行地工作?我的理解是,它并行地获取每个迷你流的总和,并将其添加到总和"中.那是对的吗 ?当流不是并行的时,它是否执行零+单流的totalSum?如果是,则对上一行代码的更准确替换将是(sum,onePartialSum)->.(sum + onePartialSum),其中sum已初始化为零???

How does this line actually work in parallel and non-parallel ? My understanding is that in parallel, it takes the sum of each mini stream and adds it to the "total sum". Is that correct ? When stream is not parallel, then does it do zero + totalSum of single stream ? If yes, then a more accurate replacement for the previous line of code will be (sum, onePartialSum) -> (sum + onePartialSum) where sum is already initialized to zero ???

推荐答案

并行:

System.out.println(Arrays.asList(1, 2, 3, 4, 5).stream().parallel()
    .reduce(/*identity*/1, /*accumulator*/(s, s2) -> s + s2, /*combiner*/(s, s2) -> s + s2));

输出:20

累加器的参数是标识和每个流的值

parameters to accumulator is identity and each stream value

工作:

1+1, 1+2, 1+3, 1+4, 1+5 // Output of accumulator is 2,3,4,5,6

组合器的参数是累加器的输出.

parameters to combiner is output of accumulator as divided.

工作:

2+3 in one thread, 4+5 in another thread and then 5/*(2+3)*/+9/*(4+5)*/ and then 14/*(5+9)*/+6 = 20

系列中:

System.out.println(Arrays.asList(1, 2, 3, 4, 5).stream()
    .reduce(/*identity*/1, /*accumulator*/(s, s2) -> s + s2, /*combiner*/(s, s2) -> s + s2));

输出:16

在串联模式下,组合器将不会被执行,累加器的参数是标识,每个流的值均被设置为标识

In series mode, combiner won't be excuted and parameters to accumulator are identity and each stream value but result of each is set to identity

工作:

1(identity) + 1(first element) = 2(new identity)
2(new identity) + 2(second element) = 4(new identity)
4(new identity) + 3(third element) = 7(new identity)
7(new identity) + 4(third element) = 11(new identity)
11(new identity) + 5(third element) = 16(new identity)

希望您能理解

这篇关于了解Java Stream Reduce-并行vs系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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