Summary
New default methods min() and max() are added to the Comparator interface, which allow finding greater or smaller of two objects, according to this Comparator.
Problem
While we have Stream.min/max and Collections.min/max, often it's required to select a greater or smaller object of two. Doing this with existing APIs is unnecessarily complicated. E.g., given two objects a and b and a comparator comp, we have the following possibilities to find the maximal object:
comp.compare(a, b) > 0 ? a : b
This explicit version mentions both
a
andb
twice and not very readable, as it requires matching the sign (>
or<
) with right-hand operands of ?: to understand whether we are looking for maximum or minimumStream.of(a, b).max(comp).get()
Creates an unnecessary
Optional
which is always present and should be explicitly unwrapped. Also, depending on JIT compiler used and compilation tier, it may create unnecessary performance overhead and heap objects.BinaryOperator.maxBy(comp).apply(a, b)
While being the longest in terms of characters, it's probably the best of existing alternatives. However, it's not very discoverable and creates an unnecessary function when we just want to get the value.
Solution
It's suggested to extend the Comparator
interface adding two default methods:
public interface Comparator<T> {
...
default T max(T a, T b) { ... }
default T min(T a, T b) { ... }
}
The solution was preliminary discussed in core-libs-dev in this thread: https://mail.openjdk.org/pipermail/core-libs-dev/2025-May/145638.html
Specification
/**
* Returns the greater of two values according to this comparator.
* If the arguments are equal with respect to this comparator,
* the {@code a} argument is returned.
*
* @param a an argument.
* @param b another argument.
* @return the larger of {@code a} and {@code b} according to this comparator.
* @throws ClassCastException if the collection contains elements that are
* not <i>mutually comparable</i> (for example, strings and
* integers).
*
* @since 25
*/
default T max(T a, T b) { ... }
/**
* Returns the smaller of two values according to this comparator.
* If the arguments are equal with respect to this comparator,
* the {@code a} argument is returned.
*
* @param a an argument.
* @param b another argument.
* @return the smaller of {@code a} and {@code b} according to this comparator.
* @throws ClassCastException if the collection contains elements that are
* not <i>mutually comparable</i> (for example, strings and
* integers).
*
* @since 25
*/
default T min(T a, T b) { ... }
- csr of
-
JDK-8356995 Provide default methods min(T, T) and max(T, T) in Comparator interface
-
- New
-