DEV Community

realNameHidden
realNameHidden

Posted on

Collections.max() method in Java

The Collections.max method is part of the Java Collections Framework and is used to find the maximum element in a given collection. It can determine the maximum element using either the natural ordering of the elements or a specified Comparator.

Description of Collections.max

Method Signatures

Natural Ordering

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

Enter fullscreen mode Exit fullscreen mode

Parameters:

coll: The collection from which the maximum element is to be determined. All elements in the collection must implement the Comparable interface.

Returns: The maximum element according to the natural ordering.

Throws:
ClassCastException if the elements are not mutually comparable.
NoSuchElementException if the collection is empty.

Custom Comparator

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

Enter fullscreen mode Exit fullscreen mode

Parameters:
coll: The collection from which the maximum element is to be determined.

comp: The comparator to determine the order of the collection. A null comparator indicates that natural ordering should be used.

Returns: The maximum element according to the specified comparator.

Throws:
NoSuchElementException if the collection is empty.

Example of Collections.max

Below are examples demonstrating how to use the Collections.max method with both natural ordering and a custom comparator.

Example 1: Using Natural Ordering
In this example, we find the maximum element from a list of integers using their natural ordering.

import java.util.*;

public class MaxExampleNaturalOrdering {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(3, 9, 1, 5, 8);

        // Find the maximum element using natural ordering
        int maxNumber = Collections.max(numbers);

        System.out.println("Maximum number: " + maxNumber); // Output: 9
    }
}

Enter fullscreen mode Exit fullscreen mode

Example 2: Using Custom Comparator
In this example, we find the maximum element from a list of strings based on their lengths using a custom comparator.

import java.util.*;

public class MaxExampleCustomComparator {
    public static void main(String[] args) {
        List<String> fruits = Arrays.asList("Banana", "Apple", "Mango", "Cherry");

        // Find the maximum element using a custom comparator based on string length
        String maxFruitByLength = Collections.max(fruits, Comparator.comparingInt(String::length));

        System.out.println("Maximum fruit by length: " + maxFruitByLength); // Output: Banana
    }
}

Enter fullscreen mode Exit fullscreen mode

Explanation

Natural Ordering:
In the first example, Collections.max(numbers) uses the natural ordering of integers (which implement the Comparable interface) to find the maximum value.

Custom Comparator:
In the second example, Collections.max(fruits, Comparator.comparingInt(String::length)) uses a custom comparator that compares the lengths of the strings to determine which string is the longest.

The Collections.max method is useful for determining the largest element in a collection of any type, as long as the elements can be compared either through natural ordering or a specified comparator. This method is particularly helpful when you need to sort or organize data dynamically based on different criteria.

Top comments (0)