當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。