📜  Java中的 Stream.reduce() 示例

📅  最后修改于: 2022-05-13 01:54:57.593000             🧑  作者: Mango

Java中的 Stream.reduce() 示例

很多时候,我们需要执行将流归约为单个结果值的操作,例如,最大值、最小值、总和、乘积等。归约是组合所有元素的重复过程。

reduce操作将二元运算符应用于流中的每个元素,其中运算符的第一个参数是前一个应用程序的返回值,第二个参数是当前流元素。

句法 :

T reduce(T identity, BinaryOperator accumulator);

Where, identity is initial value 
of type T and accumulator is a 
function for combining two values.

sum()、min()、max()、count() 等是 reduce 操作的一些示例。 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 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 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_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 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