當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Stream.max()用法及代碼示例


Stream.max()根據提供的Comparator返回流的最大元素。比較器是一種比較函數,它對某些對象集合施加總排序。 max()是一種終端操作,它組合流元素並返回摘要結果。因此,max()是歸約的一種特殊情況。該方法返回Optional實例。

用法:

Optional<T> max(Comparator<? super T> 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

異常:如果最大元素為null,則此方法引發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<Integer> 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<Integer> 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<Integer> 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<String> 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<String> 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


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Stream.max() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。