0
0
JavaHow-ToBeginner · 3 min read

How to Use Collections.max in Java: Syntax and Examples

Use Collections.max(Collection<T> coll) to find the largest element in a collection in Java. It returns the maximum element according to the natural ordering or a provided comparator.
📐

Syntax

The Collections.max method has two main forms:

  • Collections.max(Collection<T> coll): Finds the maximum element using natural order.
  • Collections.max(Collection<T> coll, Comparator<? super T> comp): Finds the maximum element using a custom comparator.

Here, Collection<T> is any collection like a list or set, and T must be comparable or you must provide a comparator.

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

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

Example

This example shows how to find the maximum number in a list using natural order, and how to find the maximum string by length using a custom comparator.

java
import java.util.*;

public class MaxExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(3, 7, 2, 9, 5);
        int maxNumber = Collections.max(numbers);
        System.out.println("Max number: " + maxNumber);

        List<String> words = Arrays.asList("apple", "banana", "pear", "grape");
        String longestWord = Collections.max(words, Comparator.comparingInt(String::length));
        System.out.println("Longest word: " + longestWord);
    }
}
Output
Max number: 9 Longest word: banana
⚠️

Common Pitfalls

  • Passing an empty collection causes NoSuchElementException.
  • Using Collections.max on elements that do not implement Comparable without a comparator causes ClassCastException.
  • For custom objects, always provide a comparator or implement Comparable.
java
import java.util.*;

class Person {
    String name;
    int age;
    Person(String name, int age) { this.name = name; this.age = age; }
}

public class MaxPitfall {
    public static void main(String[] args) {
        List<Person> people = Arrays.asList(new Person("Alice", 30), new Person("Bob", 25));
        // This will throw ClassCastException because Person does not implement Comparable
        // Person oldest = Collections.max(people);

        // Correct way: provide a comparator
        Person oldest = Collections.max(people, Comparator.comparingInt(p -> p.age));
        System.out.println("Oldest person: " + oldest.name);
    }
}
Output
Oldest person: Alice
📊

Quick Reference

Remember these tips when using Collections.max:

  • Use natural ordering if elements implement Comparable.
  • Use a comparator for custom ordering or non-comparable elements.
  • Do not use on empty collections.

Key Takeaways

Collections.max returns the largest element in a collection based on natural order or a comparator.
Always ensure the collection is not empty to avoid exceptions.
Provide a comparator for custom objects that do not implement Comparable.
Use Comparator.comparing or lambda expressions for custom comparison logic.
Collections.max works with any Collection type like List or Set.