stream sum java

‮sptth‬://www.lautturi.com
stream sum java

To sum the elements of a Stream in Java, you can use the reduce() method of the Stream interface. This method allows you to combine all the elements of the stream into a single value, using a specified binary operator.

For example, to sum the elements of a Stream of integers, you can use the following code:

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
int sum = stream.reduce(0, (a, b) -> a + b);
System.out.println(sum); // Output: 15
Created Time:2017-10-17 20:22:37  Author:lautturi