当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。