How to use Java 8 Stream Reduce Examples with List and Array

java-8-tutorial-stream-api-reduce-feature-image


Java 8 provides an extremely powerful abstract concept Stream with many useful mechanics for consuming and processing data in Java Collection. In the tutorial, We will do lots of examples to explore more the helpful of Stream API with reduction operation on the specific topic: “Java 8 Stream Reduce Collection”.

What will we do?

  • Explain how Java 8 Stream Reduce work?
  • Apply Stream Reduction on Java List, Array

Now let’s do examples for more details!

Related posts:
Java 8 Stream FlatMap Examples
Stream Filter in Java 8

Java Stream Reduce

Java Stream Reduction is a terminal operation that aggregates a Java Stream then returns one value by combining the contents of a stream.

Many reduction operations perform a specific task such as: average, sum, min, max, and count.

Examples:

+ Initial Java List:

+ How to sum all Element of a Stream?

+ How to get average value of an Integer List?

+ How to get Min/Max value of an Integer List?

The JDK also provides a general-purpose reduction operation Stream.reduce().
Now Let’s through all detail signatures of Java Stream Reduce APIs:

Stream.reduce Method

Stream.reduce(BinaryOperator accumulator)

Signature method:

-> It excutes a reduction on the elements of this stream, using an associative accumulation function.

According to Oracle, this is equivalent to:

accumulator function has two parameters: a partial result of the reduction operation and nexting element of the stream.

Example Stream Reduce an Integer List:

Stream.reduce(T identity, BinaryOperator accumulator)

Signature method:

-> It performs a reduction on each elements of this stream, it uses the provided identity value and an associative accumulation function, then returns the reduced value.

Identity is used to init the value of the reduction operation and also the default result if the stream is empty

– Example:

-> Explain: 65 = 10 + 1 + 3 + 5 + 2 + 9 + 8 + 15 + 12

Stream.reduce(identity, accumulator, combiner)

Signature method:

Combiner is an associative, non-interfering, stateless function for combining two values.

When we use Combiner function:
+ In case Parallel Stream, the function is used to combine the partial result of the reduction operation.
+ When having mismatch types between the accumulator arguments and the accumulator implementation.

Example:

Stream Reduce Examples

Stream Reduce String List Example

– Example: How to concatenate strings in a string list with Java Stream Reduce?

Stream Reduce Custom Object List Example

– Create Java Developer class:

– Initial Developer List:

– Example: How to calculate a total annual salary of all developers in a list?

-> We got an Error of Compiler. Why?
Because the mismatch types between arguments of accumulator function and the accumulator function’s implementation.

(Developer, Double) -> Double + Double

How to resolve it?
-> In the case, we use the Combiner function: Double::sum

Handle Stream Reduce Exception

We can get an exception when working with Stream.reduce():

-> java.lang.ArithmeticException: / by zero

We can handle the Exception by Logging as below implementation:

Parallel Stream Reduce

Java Stream provides a parallel API processing parallelStream(), we can combine it with reduce() method to leverage the multiple cores on your computer for performance processing data

To combine a partial result of the reduction operation in parallel mode, we must use the Combiner function with reduce() operation.

– Example:

Stream Reduce Array Objects Example

To apply Stream Reduction in Array with Java 8, We do 2 steps:

  • Create Stream from Array Objects.
  • Apply Stream reduce() for Array Objects as the same way we had done with above List Objects.

Example:

Conclusion

We had learned Java 8 Stream Reduction with Examples:

  • Apply Java 8 Stream Reduce API to Integer, String and Custom Object List and Array
  • Combine Java Parallel Stream with Stream Reduction to improve performance
  • Handling exception when working with Java Stream Reduction

Thanks for reading! See you later!

By grokonez | April 11, 2019.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*