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


Java Stream min()用法及代碼示例


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

用法:

Optional<T> min(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:整數列表中的最小值。

// Java code for Stream.min() method to get 
// the minimum element of the Stream 
// according to the provided Comparator. 
import java.util.*; 
  
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.min() to get minimum 
        // element according to provided Integer Comparator 
        Integer var = list.stream().min(Integer::compare).get(); 
  
        System.out.print(var); 
    } 
}

輸出:

-18

示例2:使用min()函數反向比較器以獲得最大值。

// Java code for Stream.min() method 
// to get the minimum element of the  
// Stream according to provided comparator. 
import java.util.*; 
  
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.min() with reverse 
        // comparator to get maximum element. 
        Optional<Integer> var = list.stream() 
                    .min(Comparator.reverseOrder()); 
  
        // IF var is empty, then output will be Optional.empty 
        // else value in var is printed. 
        if(var.isPresent()){ 
        System.out.println(var.get()); 
        } 
        else{ 
            System.out.println("NULL"); 
        } 
          
    } 
}

輸出:

25

示例3:根據最後一個字符比較字符串。

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

輸出:

for


相關用法


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