Skip to content

8356995: Provide default methods min(T, T) and max(T, T) in Comparator interface #25297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/java.base/share/classes/java/util/Comparator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,7 +30,6 @@
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;
import java.util.Comparators;

/**
* A comparison function, which imposes a <i>total ordering</i> on
Expand Down Expand Up @@ -189,6 +188,42 @@ default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}

/**
* Returns the greater of two values according to this comparator.
* If the arguments are equal with respect to this comparator,
* the {@code b} 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What 'collection' meaned here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you. The description was copied from Collections::max, which is not so appropriate here. I think it's better to copy it from Comparator::compare (also, added @throws NPE for consistency).

* not <i>mutually comparable</i> (for example, strings and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<em> is more intent revealing.

Suggested change
* not <i>mutually comparable</i> (for example, strings and
* not <em>mutually comparable</em> (for example, strings and

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this sentence from java.util.Collections where <i> is used 48 times. I'll replace it here with <em>, but probably it should be updated en-masse for consistency?

* integers).
*
* @since 25
*/
default T max(T a, T b) {
return compare(a, b) > 0 ? a : 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) {
return compare(a, b) > 0 ? b : a;
}

/**
* Returns a lexicographic-order comparator with another comparator.
* If this {@code Comparator} considers two elements equal, i.e.
Expand Down
74 changes: 74 additions & 0 deletions test/jdk/java/util/Comparator/MinMaxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/**
* @test
* @bug 8356995
* @summary Comparator min/max method tests
* @run testng MinMaxTest
*/

import org.testng.annotations.Test;

import java.util.Comparator;
import static org.testng.Assert.*;

@Test(groups = "unit")
public class MinMaxTest {
public void testMin() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to this, test methods do not need to be public (but cannot be private).

Suggested change
public void testMin() {
void testMin() {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks

Comparator<String> c = Comparator.naturalOrder();
assertEquals(c.min("a", "b"), "a");
assertEquals(c.min("b", "a"), "a");
}

public void testMax() {
Comparator<String> c = Comparator.naturalOrder();
assertEquals(c.max("a", "b"), "b");
assertEquals(c.max("b", "a"), "b");
}

public void testThrowsNPE() {
Comparator<String> c = Comparator.naturalOrder();
assertThrows(NullPointerException.class, () -> c.min(null, "a"));
assertThrows(NullPointerException.class, () -> c.min("a", null));
assertThrows(NullPointerException.class, () -> c.max(null, "a"));
assertThrows(NullPointerException.class, () -> c.max("a", null));
}

public void testThrowsCCE() {
@SuppressWarnings("unchecked")
Comparator<Object> c = (Comparator<Object>) (Comparator<?>)Comparator.naturalOrder();
assertThrows(ClassCastException.class, () -> c.min(1, "a"));
assertThrows(ClassCastException.class, () -> c.min("a", 1));
assertThrows(ClassCastException.class, () -> c.max(1, "a"));
assertThrows(ClassCastException.class, () -> c.max("a", 1));
}

public void testEqualReturnFirstOrLast() {
Comparator<Object> allEqual = (_, _) -> 0;
Object o1 = new Object();
Object o2 = new Object();
assertSame(allEqual.min(o1, o2), o1);
assertSame(allEqual.max(o1, o2), o2);
}
}