Home » Java programming language

Java Collections max() Method with Example

Collections Class max() method: Here, we are going to learn about the max() method of Collections Class with its syntax and example.
Submitted by Preeti Jain, on February 08, 2020

Collections Class max() method

Syntax:

    public static Type max(Collection co);
    public static Type max(Collection co, Comparator com);
  • max() method is available in java.util package.
  • max(Collection co) method is used to return the greatest value element of the given collection depend on natural sorting.
  • max(Collection co, Comparator com) method is used to return the greatest value element of the given collection depend on customizing sorting as given Comparator object.
  • These methods may throw an exception at the time of returning the maximum element.
    • ClassCastException: This exception may throw when the given collection elements are mutually incomparable.
    • NoSuchElementException: This exception may throw when the given collection is "blank" (i.e. no elements).
  • These are static methods and it is accessible with the class name and if we try to access these methods with the class object then also we will not get any error.

Parameter(s):

  • In the first case, max(Collection co),
    • Collection co – represents the collection object whose greatest value element of the given collection object.
  • In the first case, max(Collection co, Comparator com),
    • Collection co – represents the collection object whose greatest value element of the given collection object.
    • Comparator com – represents the Comparator with which to calculate the maximum element.

Return value:

In both the cases, the return type of the method is Type, it returns the greatest value element of the given collection depend upon the given Comparator.

Example:

// Java program to demonstrate the example 
// of max() method of Collections

import java.util.*;

public class MaxOfCollections {
    public static void main(String args[]) {
        // Instantiates an ArrayList  	   
        ArrayList arr_l = new ArrayList();

        // By using add() method is to add
        // objects in an array list 
        arr_l.add(20);
        arr_l.add(10);
        arr_l.add(50);
        arr_l.add(40);
        arr_l.add(80);

        // Display ArrayList
        System.out.println("arr_l: " + arr_l);

        // By using max(arr_l,Comparator) method is
        // to return the maximum element based on the
        // defined comparator object and here we set null
        // that means comparator follows default ordering

        System.out.print("Collections.max(arr_l,null): ");
        System.out.print(Collections.max(arr_l, null));
        System.out.println();

        // By using max(arr_l) method is
        // to return the maximum element based on the
        // natural order without using comparator object
        System.out.print("Collections.max(arr_l): ");
        System.out.print(Collections.max(arr_l));
    }
}

Output

arr_l: [20, 10, 50, 40, 80]
Collections.max(arr_l,null): 80
Collections.max(arr_l): 80



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.