Java Collections max() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.max(coll) walks through a collection once and returns its largest element according to natural order (Comparable). It works on any Collection: lists, sets, queues, or the values() of a map. The collection does not need to be sorted.

Collections.max(coll, comparator) lets you define what "largest" means, for example the longest string or the player with the highest level.

A static method of the java.util.Collections utility class, available since Java 1.2. Call it on the class name, as in Collections.max(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Pass a non-empty collection, and optionally a Comparator.

Java
List<Integer> nums = Arrays.asList(4, 9, 2);
int top = Collections.max(nums);                                     // 9
String longest = Collections.max(words, Comparator.comparingInt(String::length));
CallWhat it does
Collections.max(coll)Largest element in natural order.
Collections.max(coll, cmp)Largest element according to cmp.
Collections.max(coll, Collections.reverseOrder())Gives the smallest element.
Collections.max(map.entrySet(), Map.Entry.comparingByValue())Map entry with the largest value.

max runs in linear time and never changes the collection. The odd-looking bound T extends Object & Comparable in the declaration exists only for backward compatibility of compiled code; you can read it as "T must be Comparable".

Library declarations
Java
// java.util.Collections
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)

Parameters

ParameterDescription
collThe collection to examine. Must contain at least one element.
compThe comparator that defines the order. null means natural order.

Return Value

Return ValueDescription
TThe largest element of the collection.

Compare with Similar Methods

Related ways to find extreme values:

MethodHow it differs
min(coll)The smallest element instead of the largest.
sort(list)Orders every element; slower when you only need one.
treeSet.last()A TreeSet already knows its largest element.
reverseOrder()Flips any order, turning max into min.

Java Examples

Example 1 — Largest Number and Largest Word

Find the maximum of numbers in an ArrayDeque and of words in a list. No sorting needed.

Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

public class CollectionsMaxBasic {
    public static void main(String[] args) {
        Deque<Integer> readings = new ArrayDeque<>(Arrays.asList(18, 42, 7, 30));
        System.out.println("Highest reading: " + Collections.max(readings));

        List<String> words = Arrays.asList("pear", "Apple", "fig");
        System.out.println("Last word in natural order: " + Collections.max(words));
    }
}

How It Works

  1. Not just lists. An ArrayDeque is a Collection, so max accepts it directly.
  2. One pass. The method keeps the biggest value seen so far: 18, then 42, which neither 7 nor 30 beats.
  3. Strings by character code. "pear" starts with p, which comes after f and after the capital A, so it is the maximum.

Example 2 — Highest-Level Player with a Comparator

Objects without a natural order need a comparator. Find the player with the highest level.

Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionsMaxPlayer {
    static class Player {
        final String name;
        final int level;

        Player(String name, int level) {
            this.name = name;
            this.level = level;
        }
    }

    public static void main(String[] args) {
        List<Player> team = new ArrayList<>();
        team.add(new Player("Rin", 12));
        team.add(new Player("Kai", 31));
        team.add(new Player("Zoe", 27));

        Player best = Collections.max(team, Comparator.comparingInt(p -> p.level));
        System.out.println("Top player: " + best.name + " (level " + best.level + ")");
    }
}

How It Works

  1. No Comparable. Player has no natural order, so the one-argument max would not compile.
  2. Compare by level. Comparator.comparingInt(p -> p.level) tells max to look only at the level field.
  3. Whole object back. max returns the Player object itself, so both its name and level can be printed.

Example 3 — Empty Collections and Getting the Minimum

Calling max on an empty list throws. Also see how a reversed comparator makes max return the smallest value.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;

public class CollectionsMaxEmpty {
    public static void main(String[] args) {
        List<Integer> none = new ArrayList<>();
        try {
            Collections.max(none);
        } catch (NoSuchElementException e) {
            System.out.println("Empty list: " + e.getClass().getSimpleName());
        }
        int safe = none.isEmpty() ? 0 : Collections.max(none);
        System.out.println("Guarded result: " + safe);

        List<Integer> temps = Arrays.asList(-3, 4, -8, 1);
        System.out.println("Coldest: " + Collections.max(temps, Collections.reverseOrder()));
    }
}

How It Works

  1. Nothing to return. An empty list has no largest element, so max throws NoSuchElementException instead of returning null.
  2. Guard first. The conditional checks isEmpty() and uses 0 as a fallback, so max is never called on the empty list there.
  3. Flip the order. Under reverseOrder(), -8 counts as the "largest", which is the smallest temperature.

Frequently Asked Questions

The largest element of the collection, using natural order or the comparator you pass.
No. max scans every element once, so any order works.
max throws NoSuchElementException. Check isEmpty() first or handle the exception.
Use Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey().
Yes. It accepts any Collection, including sets, queues, and map values().

Next: min()

Return the smallest element of a collection.

min() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful