CKTIS / Java
Stream.min: practical reference
Stream.min: practical reference: min is a terminal reduction that returns the least stream element under the supplied Comparator, or Optional.empty for an empty stream.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
Stream.min(...)min is a terminal reduction that returns the least stream element under the supplied Comparator, or Optional.empty for an empty stream.
Use it to find the earliest, cheapest, or lowest-ranked object without paying to sort the entire stream.
The Comparator must be stateless, non-interfering, and consistent with the intended ordering; the contract does not select a specific instance among tied minima.
If the selected minimum is null, the method throws NullPointerException; an empty stream does not invoke the Comparator and returns Optional.empty.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
var result = java.util.stream.Stream.of(3, 1, 2).min(Integer::compareTo);
System.out.println(result.orElse(-1));
}
}
Result
1
The example exercises the documented element in a small deterministic program and compares exact standard output.