很多时候,我们需要执行将流减少到单个结果值的操作,例如,最大值,最小值,总和,乘积等。减少是组合所有元素的重复过程。
reduce操作将二进制运算符应用于流中的每个元素,其中运算符的第一个参数是前一个应用程序的返回值,第二个参数是当前流元素。
用法:
T reduce(T identity, BinaryOperator<T> accumulator); Where, identity is initial value of type T and accumulator is a function for combining two values.
sum(),min(),max(),count()等是归约运算的一些示例。 reduce()明确要求您指定如何减少流中生成的数据。
让我们看一些示例,以更好地理解reduce()函数:
示例1:
// Implementation of reduce method
// to get the longest String
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
{
// creating a list of Strings
List<String> words = Arrays.asList("GFG", "Geeks", "for",
"GeeksQuiz", "GeeksforGeeks");
// The lambda expression passed to
// reduce() method takes two Strings
// and returns the longer String.
// The result of the reduce() method is
// an Optional because the list on which
// reduce() is called may be empty.
Optional<String> longestString = words.stream()
.reduce((word1, word2)
-> word1.length() > word2.length()
? word1 : word2);
// Displaying the longest String
longestString.ifPresent(System.out::println);
}
}
输出:
GeeksforGeeks
示例2:
// Implementation of reduce method
// to get the combined String
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
{
// String array
String[] array = { "Geeks", "for", "Geeks" };
// The result of the reduce() method is
// an Optional because the list on which
// reduce() is called may be empty.
Optional<String> String_combine = Arrays.stream(array)
.reduce((str1, str2)
-> str1 + "-" + str2);
// Displaying the combined String
if (String_combine.isPresent()) {
System.out.println(String_combine.get());
}
}
}
输出:
Geeks-for-Geeks
示例3:
// Implementation of reduce method
// to get the sum of all elements
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args)
{
// Creating list of integers
List<Integer> array = Arrays.asList(-2, 0, 4, 6, 8);
// Finding sum of all elements
int sum = array.stream().reduce(0,
(element1, element2) -> element1 + element2);
// Displaying sum of all elements
System.out.println("The sum of all elements is " + sum);
}
}
输出:
The sum of all elements is 16
示例4:
// Implementation of reduce method
// to get the product of all numbers
// in given range.
import java.util.*;
import java.util.stream.IntStream;
class GFG {
// Driver code
public static void main(String[] args)
{
// To get the product of all elements
// in given range excluding the
// rightmost element
int product = IntStream.range(2, 8)
.reduce((num1, num2) -> num1 * num2)
.orElse(-1);
// Displaying the product
System.out.println("The product is : " + product);
}
}
输出:
The product is : 5040
相关用法
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java lang.Long.builtcount()用法及代码示例
- Java Java.util.concurrent.RecursiveTask用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java.util.concurrent.Phaser用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java.util.function.IntPredicate用法及代码示例
- Java Java.util.function.LongPredicate用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Stream.reduce() in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。