In Java 8 and later versions, the reduce()
method is available in the Stream
API to perform a calculation on a list of numbers or any other elements. This method allows aggregating a stream’s elements into a single result using a specified binary operator. This can be used for various calculations like addition, multiplication, maximum, minimum, etc. So, In this tutorial, we will see “How to use the ‘REDUCE’ method in Java 8?”how to use the reduce() method in Java 8 to perform a calculation on a list of numbers
How to use the reduce() method in Java 8 to perform a calculation on a list of numbers? Click To Tweet
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 | /** * 'Reduce' method in Java 8 * @author Deepak Verma * */ import java.util.Arrays; import java.util.List; public class ReduceExample_Java8 { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); // Case 1: Sum of all numbers int sum = numbers.stream() .reduce(0, (a, b) -> a + b); System.out.println("Sum: " + sum); // Case 2: Multiplication of all numbers int multiplication = numbers.stream() .reduce(1, (a, b) -> a * b); System.out.println("Multiplication: " + multiplication); // Case 3: Maximum number int max = numbers.stream() .reduce(Integer.MIN_VALUE, (a, b) -> Integer.max(a, b)); System.out.println("Maximum: " + max); // Case 4: Minimum number int min = numbers.stream() .reduce(Integer.MAX_VALUE, (a, b) -> Integer.min(a, b)); System.out.println("Minimum: " + min); } } |
Output:
1 2 3 4 | Sum: 15 Multiplication: 120 Maximum: 5 Minimum: 1 |
find first non repeated character in a string using Java 8
In the example above, we use the reduce()
method on the Stream of Integers
generated from the List of Integers
. The reduce()
method takes two arguments: the identity (starting value for the reduction) and the binary operator (lambda expression) that defines how to combine two elements. The binary operator here takes two arguments and returns the result of the operation.
Note, that for the reduce()
method, you must provide an identity value that is a neutral element for the binary operator. For example, the identity for addition is 0, and the identity for multiplication is 1.
By using the reduce()
method, you can perform various calculations on a list of numbers in a concise, robust and functional way.
Use of Reduce() method in Java 8..!!! Click To Tweet
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: