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

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

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

最大(集合<?扩展 T> coll)

Java.util.Collections类的max()方法用于根据元素的自然顺序返回给定集合的最大元素。集合中的所有元素都必须实现 Comparable 接口。此外,集合中的所有元素必须相互可比较(即,e1.compareTo(e2) 不得为集合中的任何元素 e1 和 e2 抛出 ClassCastException)。
此方法迭代整个集合,因此它需要与集合大小成正比的时间。

句法:

public static  T
  max(Collection coll)

参数:该方法将collection coll作为参数来确定其最大元素。
返回值:此方法根据元素的自然顺序返回给定集合的最大元素。

异常:此方法抛出以下异常:

  • ClassCastException –如果集合包含不可相互比较的元素(例如,字符串和整数)。
  • NoSuchElementException –如果集合为空

以下是说明max()方法的示例

示例 1:

Java
// Java program to demonstrate
// max() method for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // Adding element to Vector v
            list.add(-1);
            list.add(4);
            list.add(-5);
            list.add(1);
 
            // printing the max value
            // using max() method
            System.out.println("Max value is: "
                               + Collections.max(list));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// max() method for ClassCastException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // creating variable of object type
            Object i = Integer.valueOf(42);
 
            // Adding element to Vector v
            list.add("Hello");
            list.add((String)i);
 
            // printing the max value
            // using max() method
            System.out.println("Max value is: "
                               + Collections.max(list));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// max() method for NoSuchElementException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // printing the max value
            // using max() method
            System.out.println("Trying to get "
                               + "the max from empty list");
            System.out.println("Max value is: "
                               + Collections.max(list));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// max() method for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // Adding element to Vector v
            list.add(-1);
            list.add(4);
            list.add(-5);
            list.add(1);
 
            // printing the max value
            // using max() method
            System.out.println("Max val: "
                               + Collections.max(list,
                                                 Collections.reverseOrder()));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// max() method for ClassCastException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // creating variable of object type
            Object i = Integer.valueOf(42);
 
            // Adding element to Vector v
            list.add("Hello");
            list.add((String)i);
 
            // printing the max value
            // using max() method
            System.out.println("Max val: "
                               + Collections
                                     .max(list,
                                          Collections
                                              .reverseOrder()));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// Java program to demonstrate
// max() method for NoSuchElementException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // printing the max value
            // using max() method
            System.out.println("Trying to get "
                               + "the max from empty list");
            System.out.println("Max val: "
                               + Collections
                                     .max(list,
                                          Collections
                                              .reverseOrder()));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:
Max value is: 4

示例 2:对于ClassCastException

Java

// Java program to demonstrate
// max() method for ClassCastException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // creating variable of object type
            Object i = Integer.valueOf(42);
 
            // Adding element to Vector v
            list.add("Hello");
            list.add((String)i);
 
            // printing the max value
            // using max() method
            System.out.println("Max value is: "
                               + Collections.max(list));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Exception thrown : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

示例 3:对于NoSuchElementException

Java

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

公共静态 T max(Collection coll, Comparator comp)

Java.util.Collections类的max()方法用于根据指定比较器诱导的顺序返回给定集合的最大元素。集合中的所有元素必须通过指定的比较器相互比较(即,comp.compare(e1, e2) 不得为集合中的任何元素 e1 和 e2 抛出 ClassCastException)。
此方法迭代整个集合,因此它需要与集合大小成正比的时间。

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

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

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

异常:此方法抛出以下异常:

  • ClassCastException –如果集合包含不可相互比较的元素(例如,字符串和整数)。
  • NoSuchElementException –如果集合为空

以下是说明max()方法的示例

示例 1:

Java

// Java program to demonstrate
// max() method for Integer value
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // Adding element to Vector v
            list.add(-1);
            list.add(4);
            list.add(-5);
            list.add(1);
 
            // printing the max value
            // using max() method
            System.out.println("Max val: "
                               + Collections.max(list,
                                                 Collections.reverseOrder()));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Max val: -5

示例 2:对于ClassCastException

Java

// Java program to demonstrate
// max() method for ClassCastException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // creating variable of object type
            Object i = Integer.valueOf(42);
 
            // Adding element to Vector v
            list.add("Hello");
            list.add((String)i);
 
            // printing the max value
            // using max() method
            System.out.println("Max val: "
                               + Collections
                                     .max(list,
                                          Collections
                                              .reverseOrder()));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Exception thrown : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

示例 3:对于NoSuchElementException

Java

// Java program to demonstrate
// max() method for NoSuchElementException
 
import java.util.*;
 
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
 
        try {
 
            // creating object of LinkedList
            List list = new LinkedList();
 
            // printing the max value
            // using max() method
            System.out.println("Trying to get "
                               + "the max from empty list");
            System.out.println("Max val: "
                               + Collections
                                     .max(list,
                                          Collections
                                              .reverseOrder()));
        }
 
        catch (ClassCastException e) {
            System.out.println("Exception thrown : " + e);
        }
 
        catch (NoSuchElementException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
Trying to get the max from empty list
Exception thrown : java.util.NoSuchElementException