Collections max() method in Java with Examples
max(Collection<? extends T> coll)
The max() method of java.util.Collections class is used to return the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the collection).
This method iterates over the entire collection, hence it requires time proportional to the size of the collection.
Syntax:
public static <T extends Object & Comparable> T max(Collection coll)
Parameters: This method takes the collection coll as a parameter whose maximum element is to be determined.
Return Value: This method returns the maximum element of the given collection, according to the natural ordering of its elements.
Exception: This method throws following Exception:
- ClassCastException – if the collection contains elements that are not mutually comparable (for example, strings and integers).
- NoSuchElementException – if the collection is empty
Below are the examples to illustrate the max() method
Example 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
Example 2: for 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
Example 3: for 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)
The max() method of java.util.Collections class is used to return the maximum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection).
This method iterates over the entire collection, hence it requires time proportional to the size of the collection.
Parameters: This method takes the following argument as a parameter
- coll – the collection whose maximum element is to be determined.
- comp – the comparator with which to determine the maximum element. A null value indicates that the elements’ natural ordering should be used.
Return Value: This method returns the maximum element of the given collection, according to the specified comparator.
Exception: This method throws following Exception:
- ClassCastException – if the collection contains elements that are not mutually comparable (for example, strings and integers).
- NoSuchElementException – if the collection is empty
Below are the examples to illustrate the max() method
Example 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
Example 2: for 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
Example 3: for 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
Recommended Posts:
- Collections min() method in Java with Examples
- Collections indexOfSubList() method in Java with Examples
- Collections list() method in Java with Examples
- Collections checkedSet() method in Java with Examples
- Collections newSetFromMap() method in Java with Examples
- Collections fill() method in Java with Examples
- Collections checkedSortedMap() method in Java with Examples
- Collections checkedSortedSet() method in Java with Examples
- Collections enumeration() method in Java with Examples
- Collections copy() method in Java with Examples
- Collections replaceAll() method in Java with Examples
- Collections synchronizedSortedSet() method in Java with Examples
- Collections checkedMap() method in Java with Examples
- Collections singletonMap() method in Java with Examples
- Collections singletonList() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



