Home Java Collections Methods max() Java Collections max() Method 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.
List<Integer> nums = Arrays.asList(4, 9, 2);
int top = Collections.max(nums); // 9
String longest = Collections.max(words, Comparator.comparingInt(String::length)); Call What 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.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 Parameter Description collThe collection to examine. Must contain at least one element. compThe comparator that defines the order. null means natural order.
Return Value Return Value Description TThe largest element of the collection.
Important
An empty collection throws NoSuchElementException, so check isEmpty() first. Elements that cannot be compared throw ClassCastException, and a null element in natural order throws NullPointerException. When several elements tie for largest, do not rely on which one is returned.
Compare with Similar Methods Related ways to find extreme values:
Method How 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 1. Any collection 2. Custom order 3. Empty input Example 1 — Largest Number and Largest Word Find the maximum of numbers in an ArrayDeque and of words in a list. No sorting needed.
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));
}
} Highest reading: 42
Last word in natural order: pear How It Works Not just lists. An ArrayDeque is a Collection, so max accepts it directly.One pass. The method keeps the biggest value seen so far: 18, then 42, which neither 7 nor 30 beats.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.
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 + ")");
}
} Top player: Kai (level 31) How It Works No Comparable. Player has no natural order, so the one-argument max would not compile.Compare by level. Comparator.comparingInt(p -> p.level) tells max to look only at the level field.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.
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()));
}
} Empty list: NoSuchElementException
Guarded result: 0
Coldest: -8 How It Works Nothing to return. An empty list has no largest element, so max throws NoSuchElementException instead of returning null.Guard first. The conditional checks isEmpty() and uses 0 as a fallback, so max is never called on the empty list there.Flip the order. Under reverseOrder(), -8 counts as the "largest", which is the smallest temperature.Frequently Asked Questions What does Collections.max() return? The largest element of the collection, using natural order or the comparator you pass.
Does the collection need to be sorted for Collections.max()? No. max scans every element once, so any order works.
What happens if the collection is empty? max throws NoSuchElementException. Check isEmpty() first or handle the exception.
How do I find the map key with the highest value? Use Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey().
Can I use Collections.max() on a Set? Yes. It accepts any Collection, including sets, queues, and map values().
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion