Collection Maximum in Java
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().
Java Program
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
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.
- 1
Collections.max(numbers)walks through every element of the list exactly once. - 2At each step it compares the current element against the largest one found so far, using the elements' natural ordering (
IntegerisComparable). - 3No sorting happens at any point — unlike
binarySearch(),max()works correctly on a list in any order. - 4The single largest value found by the end of the scan is returned.
[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.
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
Approach 2: Java 8
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
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.
- 1
numbers.stream()opens a stream over the list's elements. - 2
.max(Integer::compareTo)reduces the stream to the single largest element, using the method reference as the comparison rule. - 3
.max()returns anOptional<Integer>, since a stream could in principle be empty —.get()unwraps it, safe here since the list is known to be non-empty. - 4No intermediate collection is ever built — the stream reduces directly to the one answer.
[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.
Why: max() still performs a single linear scan internally, comparing each element against the running maximum, without collecting anything.