Java ProgramsCollectionsCollection Maximum

Collection Maximum in Java

beginner·  Collections  ·  Collections Utility

Problem

Collections.max() finds the largest element of any Collection whose elements are Comparable, using a single scan with no sorting required first.

Given a list of numbers in no particular order, find the largest one using Collections.max().

Input
[12, 45, 7, 33, 29]
Output
Maximum: 45

Java Program

Java
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CollectionMaximumDemo { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(12); numbers.add(45); numbers.add(7); numbers.add(33); numbers.add(29); int max = Collections.max(numbers); System.out.println("Maximum: " + max); } }

Output

Maximum: 45

Core Logic

Scanning every element once and keeping track of the largest one seen so far finds the maximum directly, without needing the list in any particular order.

How It Works
  1. 1Collections.max(numbers) walks through every element of the list exactly once.
  2. 2At each step it compares the current element against the largest one found so far, using the elements' natural ordering (Integer is Comparable).
  3. 3No sorting happens at any point — unlike binarySearch(), max() works correctly on a list in any order.
  4. 4The single largest value found by the end of the scan is returned.
Scanning [12, 45, 7, 33, 29], 45 is the largest value encountered, so it's returned as the maximum.
💡

Key Point: Because max() never needs the list sorted, it does its job in a single linear pass — sorting the whole list just to read off the last element would do unnecessary extra work.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: Collections.max() performs a single linear scan comparing each element against the running maximum, with no sorting needed first and only one value kept in memory.

Key Concepts

Collections.max()Comparable elementslinear scan

Approach 2: Java 8

Java
import java.util.ArrayList; import java.util.List; public class CollectionMaximumStream { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(12); numbers.add(45); numbers.add(7); numbers.add(33); numbers.add(29); // Reduces the stream to its largest element, wrapped in an Optional int max = numbers.stream().max(Integer::compareTo).get(); System.out.println("Maximum: " + max); } }

Output

Maximum: 45

Core Logic

A stream's max() terminal operation reduces the whole stream down to its largest element in one call, the same idea as Collections.max() expressed as a pipeline.

How It Works
  1. 1numbers.stream() opens a stream over the list's elements.
  2. 2.max(Integer::compareTo) reduces the stream to the single largest element, using the method reference as the comparison rule.
  3. 3.max() returns an Optional&lt;Integer&gt;, since a stream could in principle be empty — .get() unwraps it, safe here since the list is known to be non-empty.
  4. 4No intermediate collection is ever built — the stream reduces directly to the one answer.
Streaming [12, 45, 7, 33, 29] through max() reduces to 45, the same result Collections.max() finds.
💡

Key Point: max() returning an Optional instead of a raw value is a deliberate difference from Collections.max(), which throws on an empty collection instead — the stream version makes the empty case something the caller has to consciously handle.

Complexity
Time Complexity: O(n)Space Complexity: O(1)

Why: max() still performs a single linear scan internally, comparing each element against the running maximum, without collecting anything.

Key Concepts

Streammax()Comparator.naturalOrder()

Related Programs