Stream.reduce() method code examples in Java
We can perform a reduction operation on elements of a Java Stream using the Stream.reduce() method that returns an Optional describing the reduced object or the reduced value itself. This post will discuss a few simple examples of the Stream.reduce() method.
1. Find maximum value of a field among custom objects
Suppose we have a Person class with name and age as its fields. We also have a list of Person objects, and the goal is to find the person having maximum age.
The idea is to create a custom method that takes two Person objects as input and returns the Person object with the maximum age. Then we create a stream of Person objects via the List.stream() method and pass the reference of the custom method to the reduce() method for the reduction operation.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import java.util.Arrays; import java.util.List; // `Person` class having `name` and `age` private fields class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public Integer getAge() { return age; } // other getters and setters @Override public String toString() { return "[" + name + ", " + String.valueOf(age) + "]"; } public static Person max(Person x, Person y) { return x.getAge() > y.getAge() ? x : y; } } // Program to find the maximum value of a field among custom objects // using `Stream.reduce()` method class Main { public static void main(String[] args) { Person p1 = new Person("George", 15); Person p2 = new Person("Tom", 10); Person p3 = new Person("Mike", 12); List<Person> users = Arrays.asList(p1, p2, p3); // get a person with the maximum age Person user = users.stream() .reduce(Person::max) .get(); System.out.println("Person with maximum age is " + user); } } |
Output:
Person with maximum age is [George, 15]
We can also directly pass a lambda function instead of using a custom method, as shown below. It takes two parameters – a partial result of the reduction (in this example, an object with the maximum age of all processed objects so far) and the next element of the stream (in this example, a Person object). It returns a new value every time it processes an element of a stream.
|
1 2 3 4 |
// get a person with the maximum age Person user = users.stream() .reduce((x, y) -> (x.getAge() > y.getAge()) ? x : y) .get(); |
There is another overloaded version of the reduce() method, which also takes an identity value along with the associative accumulation method and performs a reduction on the stream elements.
The identity element is both the initial value of the reduction and the default result if there are no elements in the stream.
|
1 2 3 4 |
// get a person with the maximum age Person user = users.stream() .reduce(new Person("Dummmy", Integer.MIN_VALUE), (x, y) -> x.getAge() > y.getAge() ? x : y); |
2. Find maximum element from a list of Integer
Suppose we have a list of Integer, and the goal is to find the maximum element in the list.
The idea is to create a stream of Integer and pass the method reference of Integer.max() to the reduce() method for the reduction operation, which then returns an Optional describing the maximum value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; // Program to find a maximum element from a list using `Stream.reduce()` // method in Java 8 and above class Main { public static Integer getMax(List<Integer> list) { return list.stream() // Stream<Integer> .reduce(Integer::max) // Optional<Integer> .get(); // Integer } public static void main(String[] args) { List<Integer> list = Arrays.asList(4, 2, 6, 5, 9, 1); System.out.println("Maximum element is " + getMax(list)); } } |
Here’s how we can use the overloaded version of the reduce() method by passing the identity element.
|
1 2 3 4 5 6 |
// Method to find a maximum element from a list of Integer in Java 8 and above public static Integer getMax(List<Integer> list) { return list.stream() .reduce(Integer.MIN_VALUE, Integer::max); } |
That’s all about the Stream.reduce() method in Java with code examples.
Exercise:
1. Find the minimum value of a field among custom objects.
2. Find the minimum element from a list of Integer.
Reference: Reduction (The Java™ Tutorials > Collections > Aggregate Operations)
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)