Last Updated : 28 Aug, 2024
In Java, the Stream.reduce()
method is used to perform a reduction on the elements of a stream using an associative accumulation function and returns an Optional
. It is commonly used to aggregate or combine elements into a single result, such as computing the maximum, minimum, sum, or product.
T reduce(T identity, BinaryOperator<T> accumulator);
T
.T
.
// Implementation of reduce method
// to get the longest String
import java.util.*;
class GFG {
public static void main(String[] args) {
// Creating a list of Strings
List<String> words = Arrays.asList("GFG", "Geeks", "for", "GeeksQuiz", "GeeksforGeeks");
// Using reduce to find the longest string in the list
Optional<String> longestString = words.stream()
.reduce((word1, word2) -> word1.length() > word2.length() ? word1 : word2);
// Displaying the longest String
longestString.ifPresent(System.out::println);
}
}
Explanation:
Optional
because the list might be empty.
// Implementation of reduce method
// to get the combined String
import java.util.*;
class GFG {
public static void main(String[] args) {
// String array to combine
String[] array = { "Geeks", "for", "Geeks" };
// Using reduce to concatenate strings with a hyphen
Optional<String> combinedString = Arrays.stream(array)
.reduce((str1, str2) -> str1 + "-" + str2);
// Displaying the combined String
combinedString.ifPresent(System.out::println);
}
}
Explanation:
Optional
because the stream might be empty.
// Implementation of reduce method
// to get the sum of all elements
import java.util.*;
class GFG {
public static void main(String[] args) {
// Creating list of integers
List<Integer> numbers = Arrays.asList(-2, 0, 4, 6, 8);
// Using reduce to find the sum of all elements
int sum = numbers.stream()
.reduce(0, (element1, element2) -> element1 + element2);
// Displaying the sum of all elements
System.out.println("The sum of all elements is " + sum);
}
}
The sum of all elements is 16
Explanation:
0
, which serves as the starting value.
// Implementation of reduce method
// to get the product of all numbers
// in given range.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
public static void main(String[] args) {
// Calculating the product of all numbers in the range [2, 8)
int product = IntStream.range(2, 8)
.reduce((num1, num2) -> num1 * num2)
.orElse(-1); // Provides -1 if the stream is empty
// Displaying the product
System.out.println("The product is : " + product);
}
}
The product is : 5040
Explanation:
-1
, used if the stream is empty.reduce
method can be used for various reduction operations.reduce
is an Optional
because the stream might be empty.reduce
requires an associative operation to combine elements.reduce
with a non-empty stream, specifying an identity value (like 0
for sum) simplifies the operation without needing to handle Optional
.These examples and explanations demonstrate how to effectively use Stream.reduce()
for different operations in Java.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4