Collection Minimum in Java
Problem
The Collections utility class provides static helper methods that operate on any List — Collections.min() scans one and returns whichever element is smallest, without the caller writing a comparison loop.
Given a List of integers in no particular order, find the smallest one.
Java Program
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionMinimum {
public static void main(String[] args) {
List<Integer> scores = Arrays.asList(78, 45, 92, 33, 61);
int lowest = Collections.min(scores); // single pass, returns the smallest element found
System.out.println("Minimum: " + lowest);
}
}Output
Core Logic
Handing the whole List to Collections.min() lets the utility method do the scanning internally, instead of tracking a running minimum by hand.
- 1
Arrays.asList(78, 45, 92, 33, 61)builds a fixed-sizeList<Integer>from the given values. - 2
Collections.min(scores)walks the whole list once, comparing each element using its natural ordering, and returns the smallest one found. - 3For
Integerelements, natural ordering is just numeric order, the same comparison<would use. - 4No sorting happens —
min()only needs a single pass to find the smallest value, not a fully ordered list.
[78, 45, 92, 33, 61], 33 is the smallest value found, so Collections.min() returns it.Key Point: Collections.min() works on any List whose elements implement Comparable — swap in Strings or any other Comparable type and the exact same call still finds the smallest one.
Why: Collections.min() makes a single linear pass over the list, comparing each element against the running minimum found so far, with no extra storage beyond that one tracked value.
Key Concepts
Approach 2: Java 8
import java.util.Arrays;
import java.util.List;
public class CollectionMinimumStream {
public static void main(String[] args) {
List<Integer> scores = Arrays.asList(78, 45, 92, 33, 61);
// Reduces the stream to its smallest element, wrapped in an Optional
int lowest = scores.stream().min(Integer::compareTo).get();
System.out.println("Minimum: " + lowest);
}
}
Output
Core Logic
A stream's min() terminal operation mirrors max() — it reduces the whole stream down to its smallest element in one call.
- 1
scores.stream()opens a stream over the list's elements. - 2
.min(Integer::compareTo)reduces the stream to the single smallest element, using the method reference as the comparison rule. - 3
.min()also returns anOptional<Integer>, so.get()unwraps it — safe here sincescoresis known to be non-empty. - 4The comparison logic is identical to
max()'s, just interpreting the result the other way.
[78, 45, 92, 33, 61] through min() reduces to 33, the same result Collections.min() finds.Key Point: Swapping max() for min() is the entire difference from the maximum version — everything else about the stream pipeline stays the same.
Why: min() still performs a single linear scan internally, comparing each element against the running minimum, without collecting anything.