📜  Java中的集合 min() 方法和示例

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

Java中的集合 min() 方法和示例

min(Collection coll)

Java.util.Collections类的min()方法用于根据元素的自然顺序返回给定集合的最小元素。集合中的所有元素都必须实现 Comparable 接口。此外,集合中的所有元素必须相互可比较(即,e1.compareTo(e2) 不得为集合中的任何元素 e1 和 e2 抛出 ClassCastException)。

此方法迭代整个集合,因此它需要与集合大小成正比的时间。

句法:

public static > T 
    min(Collection coll)

参数:该方法以collection coll作为参数,确定其最小元素

返回值:此方法根据元素的自然顺序返回给定集合的最小元素。

异常:如果集合为空,此方法将引发NoSuchElementException

下面是说明 min() 方法的示例

示例 1:

// Java program to demonstrate
// min() method
// for  Value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // create link list object
            List list = new LinkedList();
  
            // populate the list
            list.add(10);
            list.add(20);
            list.add(30);
            list.add(40);
  
            // printing the List
            System.out.println("List: " + list);
  
            // getting minimum value
            // using min() method
            int min = Collections.min(list);
  
            // printing the min value
            System.out.println("Minimum value is: " + min);
        }
  
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
List: [10, 20, 30, 40]
Minimum value is: 10

示例 2:演示NoSuchElementException

// Java program to demonstrate
// min() method for NoSuchElementException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // create link list object
            List list = new LinkedList();
  
            // printing the List
            System.out.println("List: " + list);
  
            // getting minimum value
            // using min() method
            System.out.println("Trying to get"
                               + " the minimum value "
                               + "with empty list");
  
            int min = Collections.min(list);
  
            // printing the min value
            System.out.println("Min value is: " + min);
        }
  
        catch (NoSuchElementException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
List: []
Trying to get the minimum value with empty list
Exception thrown : java.util.NoSuchElementException

min(Collection coll, Comparator comp)

Java.util.Collections类的min(Collections, Comparator)方法用于根据指定比较器诱导的顺序返回给定集合的最小元素。集合中的所有元素必须通过指定的比较器相互比较。

此方法迭代整个集合,因此它需要与集合大小成正比的时间。

参数:此方法将以下参数作为参数:

  • coll-要确定其最小元素的集合。
  • comp-用于确定最小元素的比较器。空值表示应使用元素的自然顺序。

返回值:此方法根据指定的比较器返回给定集合的最小元素

异常:如果集合为空,此方法将引发NoSuchElementException

下面是说明min()方法的示例

示例 1:

// Java program to demonstrate
// min() method
// for Integer
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // create link list object
            List list = new LinkedList();
  
            // populate the list
            list.add(10);
            list.add(20);
            list.add(30);
            list.add(40);
  
            // printing the List
            System.out.println("List: " + list);
  
            // getting minimum value
            // using min() method
            int min = Collections.min(list,
                                      Collections.reverseOrder());
  
            // printing the min value
            System.out.println("Min value by reverse order is: "
                               + min);
        }
  
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
List: [10, 20, 30, 40]
Min value by reverse order is: 40

示例 2:演示NoSuchElementException

// Java program to demonstrate
// min() method for NoSuchElementException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // create link list object
            List list = new LinkedList();
  
            // printing the List
            System.out.println("List: " + list);
  
            // getting minimum value
            // using min() method
            System.out.println("Trying to get"
                               + " the minimum value "
                               + "with empty list");
  
            int min = Collections.min(list,
                                      Collections.reverseOrder());
  
            // printing the min value
            System.out.println("Min value is: " + min);
        }
  
        catch (NoSuchElementException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
List: []
Trying to get the minimum value with empty list
Exception thrown : java.util.NoSuchElementException