RFR: 8356995: Provide default methods min(T, T) and max(T, T) in Comparator interface [v7]

Tagir F. Valeev tvaleev at openjdk.org
Sun Aug 10 17:13:13 UTC 2025


On Thu, 7 Aug 2025 16:02:28 GMT, Per Minborg <pminborg at openjdk.org> wrote:

>> Tagir F. Valeev has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Add @implSpec
>
> Maybe we can add some verbiage around why the first argument is returned if the values are equal? This relates to stable sorting algorithms and `min` and `max` can be viewed as a sorting algorithm with only two elements. So, min and max are stable in this sense and therefore amenable as a building block for more general stable sorting algorithms.

@minborg I think this would make the javadoc unnecessarily verbose. 

@rose00 yes, this totally makes sense.

@stuart-marks I see at least two 'consistency' arguments for current implementation:
1. Similar methods `BinaryOperator::minBy` and `BinaryOperator::maxBy` return both the first argument if they are equal
2. Guava's `Ordering::min` and `Ordering::max` also return the first argument, so their contract will be compatible with newly proposed methods (this is important, as `Ordering extends Comparator`). While Guava is a third-party library, it's quite popular in Java world, so it's reasonable to take it into account.

And I agree that `min(a, b) == max(b, a)` invariant does not look very useful in practice. In general, intuition expects the first argument to be returned (see the @minborg 's note about stable sorting).

-------------

PR Comment: https://git.openjdk.org/jdk/pull/25297#issuecomment-3172773692


More information about the core-libs-dev mailing list