📜  在Java中使用示例流 min() 方法

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

在Java中使用示例流 min() 方法

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

句法 :

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

// 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 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 list = Arrays.asList(-9, -18, 0, 25, 4);
  
        // Using Stream.min() with reverse
        // comparator to get maximum element.
        Optional 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 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