📜  Java中的 Stream.max() 方法和示例

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

Java中的 Stream.max() 方法和示例

Stream.max() 根据提供的比较器返回流的最大元素。 Comparator 是一个比较函数,它对某些对象集合进行总排序。 max() 是一个终端操作,它结合流元素并返回一个汇总结果。因此,max() 是归约的一种特殊情况。该方法返回可选实例。

句法 :

Optional max(Comparator comparator)

Where, Optional is a container object which
may or may not contain a non-null value 
and T is the type of objects
that may be compared by this comparator

异常:如果最大元素为空,则此方法抛出NullPointerException

示例 1:

// Implementation of Stream.max()
// to get the maximum element
// of the Stream according to the
// provided Comparator.
import java.util.*;
import java.util.Optional;
import java.util.Comparator;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of integers
        List list = Arrays.asList(-9, -18, 0, 25, 4);
  
        System.out.print("The maximum value is : ");
  
        // Using stream.max() to get maximum
        // element according to provided Comparator
        // and storing in variable var
        Integer var = list.stream().max(Integer::compare).get();
  
        System.out.print(var);
    }
}

输出 :

The maximum value is : 25

示例 2:

// Implementation of Stream.max()
// to get the maximum element
// of the Stream according to the
// provided Comparator.
import java.util.*;
import java.util.Optional;
import java.util.Comparator;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of integers
        List list = Arrays.asList(-9, -18, 0, 25, 4);
  
        // Using stream.max() to get maximum
        // element according to provided Comparator
        // Here, the smallest element in list
        // will be stored in variable var
        Optional var = list.stream()
                     .max(Comparator.reverseOrder());
  
        // If a value is present, isPresent()
        // will return true, else display message
        if (var.isPresent()) {
            System.out.println(var.get());
        }
        else {
            System.out.println("-1");
        }
    }
}

输出 :

-18

示例 3:

// Implementation of Stream.max()
// to get the maximum element
// of the Stream according to the
// provided Comparator.
import java.util.*;
import java.util.Optional;
import java.util.Comparator;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of Strings
        List list = Arrays.asList("G", "E", "E", "K",
                                          "g", "e", "e", "k");
  
        // using Stream.max() method with Comparator
        // Here, the character with maximum ASCII value
        // is stored in variable MAX
        String MAX = list.stream().max(Comparator.
                       comparing(String::valueOf)).get();
  
        // Displaying the maximum element in
        // the stream according to provided Comparator
        System.out.println("Maximum element in the "
                           + "stream is : " + MAX);
    }
}

输出 :

Maximum element in the stream is : k

示例 4:

// Implementation of Stream.max()
// to get the maximum element
// of the Stream according to the
// provided Comparator.
import java.util.*;
import java.util.Optional;
import java.util.Comparator;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // creating an array of strings
        String[] array = { "Geeks", "for", "GeeksforGeeks",
                           "GeeksQuiz" };
  
        // Here, the Comparator compares the strings
        // based on their last characters and returns
        // the maximum value accordingly
        // The result is stored in variable MAX
        Optional MAX = Arrays.stream(array).max((str1, str2) ->  
                       Character.compare(str1.charAt(str1.length() - 1), 
                                       str2.charAt(str2.length() - 1)));
  
        // If a value is present,
        // isPresent() will return true
        if (MAX.isPresent()) 
            System.out.println(MAX.get());        
        else 
            System.out.println("-1");        
    }
}

输出 :

GeeksQuiz