Tutoriel JAVA8 N°13: Stream avancé partie 5 Reduce .

Опубликовано: 31 Июль 2018
на канале: Simad
92
2

On this page we will provide java 8 Stream reduce() example. It is used to get the sum of numbers stored in List, Array etc. It can also concatenate the string stored in List and Array etc. Java 8 lambda Stream.reduce() method can perform many more reducing task as required. We are providing here some usability of Stream.reduce(). Find the examples.

Stream.reduce() with Accumulator
Here we will pass BinaryOperator as accumulator. In case of numeric BinaryOperator, the start value will be 0. In case of string, the start value will be a blank string.

reduce(BinaryOperator accumulator)

The method will return Optional instance. Find the example.
ReduceDemo1.java

Stream.reduce() with Identity and Accumulator
Here will use an identity and accumulator. We will pass the identity as start value.

Find the example.

Stream.reduce() with Identity, Accumulator and Combiner
Here we will pass three arguments identity, accumulator and combiner in reduce() method. This method with these three arguments is used in parallel processing. Combiner works with parallel stream only, otherwise there is nothing to combine.


Find the example.