Java Collections.max Method
Last modified: April 20, 2025
The Collections.max
method is a utility method from the
java.util.Collections
class. It returns the maximum element of
the given collection according to natural ordering. The collection must
implement the Comparable
interface.
For custom ordering, you can provide a Comparator
. The method
throws exceptions for empty collections or incompatible elements. It's a
convenient way to find maximum values without manual iteration.
Collections.max Method Overview
The Collections.max
method has two variants. The first takes a
collection with comparable elements. The second takes a collection and a
comparator for custom ordering. Both throw NoSuchElementException
for empty collections.
Elements must be mutually comparable. The method performs a linear search through the collection. It's useful for finding extremes in datasets. The method is generic and works with any non-empty collection.
Finding Maximum in List of Integers
This example demonstrates finding the maximum value in a list of integers.
Integers implement Comparable
, so no comparator is needed. The
example shows basic usage with a simple numeric collection.
package com.zetcode; import java.util.Arrays; import java.util.Collections; import java.util.List; public class MaxIntegerList { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(14, 2, 73, 4, 25, 6); Integer max = Collections.max(numbers); System.out.println("Numbers: " + numbers); System.out.println("Maximum value: " + max); } }
This code creates a list of integers and finds the maximum value. The
Collections.max
method scans the list and returns 73 as the
largest element. The output shows both the original list and the maximum.
The example demonstrates the simplest case where elements implement
Comparable
. No additional parameters are needed for natural
ordering comparisons.
Finding Maximum in List of Strings
Strings also implement Comparable
, allowing natural ordering
comparisons. This example finds the lexicographically maximum string in a
list. The comparison is case-sensitive and based on Unicode values.
package com.zetcode; import java.util.Arrays; import java.util.Collections; import java.util.List; public class MaxStringList { public static void main(String[] args) { List<String> words = Arrays.asList("apple", "Orange", "banana", "kiwi"); String max = Collections.max(words); System.out.println("Words: " + words); System.out.println("Maximum word: " + max); } }
The code finds the maximum string in a list of fruits. "Orange" is returned as maximum because 'O' has higher Unicode value than lowercase letters. The output shows the original list and the maximum string.
Note that string comparison is case-sensitive. For case-insensitive comparison, we would need to provide a custom comparator, as shown in later examples.
Using Custom Comparator
This example demonstrates using a custom comparator to find the maximum element. We create a comparator that compares string lengths. The longest string will be returned as maximum.
package com.zetcode; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class MaxWithComparator { public static void main(String[] args) { List<String> words = Arrays.asList("apple", "Orange", "banana", "kiwi"); Comparator<String> lengthComparator = Comparator.comparing(String::length); String max = Collections.max(words, lengthComparator); System.out.println("Words: " + words); System.out.println("Longest word: " + max); } }
The code creates a comparator that compares strings by length. "banana" is returned as the longest word. The output shows both the original list and the maximum-length string.
Custom comparators allow flexible definitions of "maximum" beyond natural ordering. This is powerful for complex objects or special comparison logic.
Finding Maximum in Set
The Collections.max
method works with any Collection
,
including sets. This example demonstrates finding the maximum value in a
HashSet
of doubles.
package com.zetcode; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class MaxDoubleSet { public static void main(String[] args) { Set<Double> numbers = new HashSet<>(); Collections.addAll(numbers, 3.14, 2.71, 1.62, 9.81, 0.58); Double max = Collections.max(numbers); System.out.println("Numbers: " + numbers); System.out.println("Maximum value: " + max); } }
The code creates a set of double values and finds the maximum. Since sets are
unordered, the output order may vary. However, Collections.max
correctly identifies 9.81 as the largest value.
This demonstrates that Collections.max
works with any collection
type, not just lists. The method only requires that elements are comparable.
Case-Insensitive String Maximum
This example shows how to find the maximum string while ignoring case. We use
String.CASE_INSENSITIVE_ORDER
comparator. This provides consistent
ordering regardless of letter case.
package com.zetcode; import java.util.Arrays; import java.util.Collections; import java.util.List; public class MaxCaseInsensitive { public static void main(String[] args) { List<String> words = Arrays.asList("apple", "Orange", "banana", "kiwi"); String max = Collections.max(words, String.CASE_INSENSITIVE_ORDER); System.out.println("Words: " + words); System.out.println("Maximum word (case-insensitive): " + max); } }
The code finds the maximum string while ignoring case differences. "Orange" is returned as maximum because 'O' equals 'o' in case-insensitive comparison. The output shows the original list and the case-insensitive maximum.
This demonstrates using built-in comparators for common comparison scenarios.
The String
class provides several useful comparators for text
processing.
Finding Maximum of Custom Objects
This example demonstrates finding the maximum of custom objects. We create a
Person
class and find the oldest person using a comparator based
on age.
package com.zetcode; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; class Person { String name; int age; Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return name + " (" + age + ")"; } } public class MaxCustomObjects { public static void main(String[] args) { List<Person> people = Arrays.asList( new Person("Alice", 25), new Person("Bob", 30), new Person("Charlie", 20) ); Comparator<Person> ageComparator = Comparator.comparingInt(p -> p.age); Person oldest = Collections.max(people, ageComparator); System.out.println("People: " + people); System.out.println("Oldest person: " + oldest); } }
The code defines a Person
class with name and age fields. We
create a comparator that compares people by age. Bob (age 30) is identified
as the oldest person in the list.
This demonstrates how Collections.max
can work with any object
type when provided with an appropriate comparator. The output shows all people
and the identified maximum.
Handling Empty Collection
This example shows what happens when trying to find the maximum of an empty
collection. The Collections.max
method throws
NoSuchElementException
in this case.
package com.zetcode; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class MaxEmptyCollection { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); try { Integer max = Collections.max(numbers); System.out.println("Maximum value: " + max); } catch (Exception e) { System.out.println("Error: " + e.getClass().getSimpleName()); System.out.println("Message: " + e.getMessage()); } } }
The code attempts to find the maximum of an empty list. As expected, this
throws a NoSuchElementException
. The output shows the exception
type and message.
This demonstrates the importance of checking collection size before calling
Collections.max
. Alternatively, you could provide a default
value when the collection might be empty.
Source
Java Collections.max Documentation
In this article, we've explored the Java Collections.max
method
in depth. We've covered basic usage with different types, custom comparators,
and edge cases. This method provides a convenient way to find maximum elements
in collections.
Author
List all Java tutorials.