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


Java Collections max()用法及代码示例


max(Collection<? extends T> coll)

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

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

用法:


public static <T extends Object & Comparable> T
  max(Collection coll)

参数:此方法将collection coll作为要确定其最大元素的参数。

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

异常:此方法引发以下异常:

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

以下示例说明了max()方法

示例1:

// 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<Integer> list = new LinkedList<Integer>(); 
  
            // Adding element to Vector v 
            list.add(-1); 
            list.add(4); 
            list.add(-5); 
            list.add(1); 
  
            // prining 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); 
        } 
    } 
}
输出:
Max value is: 4

示例2:对于ClassCastException

// 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<String> list = new LinkedList<String>(); 
  
            // creating variable of object type 
            Object i = Integer.valueOf(42); 
  
            // Adding element to Vector v 
            list.add("Hello"); 
            list.add((String)i); 
  
            // prining 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 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<Integer> list = new LinkedList<Integer>(); 
  
            // prining 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

public static T max(Collection<? extends T> coll, Comparator<? super T> comp)

java.util.Collections类的max()方法用于根据指定比较器引发的顺序返回给定集合的最大元素。集合中的所有元素必须可以通过指定的比较器相互比较(即,comp.compare(e1,e2)不得对集合中的任何元素e1和e2抛出ClassCastException。

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

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

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

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

异常:此方法引发以下异常:

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

以下示例说明了max()方法

示例1:

// 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<Integer> list = new LinkedList<Integer>(); 
  
            // Adding element to Vector v 
            list.add(-1); 
            list.add(4); 
            list.add(-5); 
            list.add(1); 
  
            // prining 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 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<String> list = new LinkedList<String>(); 
  
            // creating variable of object type 
            Object i = Integer.valueOf(42); 
  
            // Adding element to Vector v 
            list.add("Hello"); 
            list.add((String)i); 
  
            // prining 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 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<Integer> list = new LinkedList<Integer>(); 
  
            // prining 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


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Collections max() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。