Java 8 reduce operation

In this post we will demonstrate how to use the reduce operation on a stream in Java 8. The reduce operation on a stream will return a single value based on your reduce criteria.

Reduce Operation Examples

The example below uses the reduce function to sum all the values in the list. The second example passes an identity parameter ’20’ which is your initial value.

Running the above code produces the following output

The following examples runs max and min reduce functions to find the maximum and minimum value in the list.

Running the above code produces the following

In conclusion I hope you found this post useful and it has given you an insight on how to use the reduce function on a java 8 stream.

Similar posts:

  1. Java 8 flatmap examples
  2. Java 8 filter examples
  3. Java 8 map examples
  4. How to parse a JSON file using GSON

References:

  1. Java 8 API Documentation

 

 

 

 

Leave a Reply

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