diff --git a/jdk/make/java/java/FILES_java.gmk b/jdk/make/java/java/FILES_java.gmk
index 8c2f80c5248..189b9fcb7c2 100644
--- a/jdk/make/java/java/FILES_java.gmk
+++ b/jdk/make/java/java/FILES_java.gmk
@@ -257,6 +257,7 @@ JAVA_JAVA_java = \
sun/util/calendar/ZoneInfoFile.java \
java/util/TooManyListenersException.java \
java/util/Comparator.java \
+ java/util/Comparators.java \
java/util/Collections.java \
java/util/Iterator.java \
java/util/ListIterator.java \
diff --git a/jdk/src/share/classes/java/util/Collections.java b/jdk/src/share/classes/java/util/Collections.java
index 58eae6f9509..b6bac484dac 100644
--- a/jdk/src/share/classes/java/util/Collections.java
+++ b/jdk/src/share/classes/java/util/Collections.java
@@ -3759,7 +3759,7 @@ public class Collections {
return c2.compareTo(c1);
}
- private Object readResolve() { return reverseOrder(); }
+ private Object readResolve() { return Collections.reverseOrder(); }
}
/**
diff --git a/jdk/src/share/classes/java/util/Comparator.java b/jdk/src/share/classes/java/util/Comparator.java
index 35ead373b82..017c2e78e2c 100644
--- a/jdk/src/share/classes/java/util/Comparator.java
+++ b/jdk/src/share/classes/java/util/Comparator.java
@@ -25,6 +25,11 @@
package java.util;
+import java.util.function.Function;
+import java.util.function.ToIntFunction;
+import java.util.function.ToLongFunction;
+import java.util.function.ToDoubleFunction;
+
/**
* A comparison function, which imposes a total ordering on some
* collection of objects. Comparators can be passed to a sort method (such
@@ -165,4 +170,93 @@ public interface Comparator {
* @see Object#hashCode()
*/
boolean equals(Object obj);
+
+ /**
+ * Returns a comparator that imposes the reverse ordering of this
+ * comparator.
+ *
+ * @return A comparator that imposes the reverse ordering of this
+ * comparator.
+ * @since 1.8
+ */
+ default Comparator reverseOrder() {
+ return Collections.reverseOrder(this);
+ }
+
+ /**
+ * Constructs a lexicographic order comparator with another comparator.
+ * For example, a {@code Comparator byLastName} can be composed
+ * with another {@code Comparator byFirstName}, then {@code
+ * byLastName.thenComparing(byFirstName)} creates a {@code
+ * Comparator} which sorts by last name, and for equal last names
+ * sorts by first name.
+ *
+ * @param other the other comparator used when equals on this.
+ * @throws NullPointerException if the argument is null.
+ * @since 1.8
+ */
+ default Comparator thenComparing(Comparator super T> other) {
+ return Comparators.compose(this, other);
+ }
+
+ /**
+ * Constructs a lexicographic order comparator with a function that
+ * extracts a {@code Comparable} key. This default implementation calls
+ * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
+ *
+ * @param the {@link Comparable} type for comparison
+ * @param keyExtractor the function used to extract the {@link Comparable} sort key
+ * @throws NullPointerException if the argument is null.
+ * @see Comparators#comparing(Function)
+ * @see #thenComparing(Comparator)
+ * @since 1.8
+ */
+ default > Comparator thenComparing(Function super T, ? extends U> keyExtractor) {
+ return thenComparing(Comparators.comparing(keyExtractor));
+ }
+
+ /**
+ * Constructs a lexicographic order comparator with a function that
+ * extracts a {@code int} value. This default implementation calls {@code
+ * thenComparing(this, Comparators.comparing(keyExtractor))}.
+ *
+ * @param keyExtractor the function used to extract the integer value
+ * @throws NullPointerException if the argument is null.
+ * @see Comparators#comparing(ToIntFunction)
+ * @see #thenComparing(Comparator)
+ * @since 1.8
+ */
+ default Comparator thenComparing(ToIntFunction super T> keyExtractor) {
+ return thenComparing(Comparators.comparing(keyExtractor));
+ }
+
+ /**
+ * Constructs a lexicographic order comparator with a function that
+ * extracts a {@code long} value. This default implementation calls
+ * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
+ *
+ * @param keyExtractor the function used to extract the long value
+ * @throws NullPointerException if the argument is null.
+ * @see Comparators#comparing(ToLongFunction)
+ * @see #thenComparing(Comparator)
+ * @since 1.8
+ */
+ default Comparator thenComparing(ToLongFunction super T> keyExtractor) {
+ return thenComparing(Comparators.comparing(keyExtractor));
+ }
+
+ /**
+ * Constructs a lexicographic order comparator with a function that
+ * extracts a {@code double} value. This default implementation calls
+ * {@code thenComparing(this, Comparators.comparing(keyExtractor))}.
+ *
+ * @param keyExtractor the function used to extract the double value
+ * @throws NullPointerException if the argument is null.
+ * @see Comparators#comparing(ToDoubleFunction)
+ * @see #thenComparing(Comparator)
+ * @since 1.8
+ */
+ default Comparator thenComparing(ToDoubleFunction super T> keyExtractor) {
+ return thenComparing(Comparators.comparing(keyExtractor));
+ }
}
diff --git a/jdk/src/share/classes/java/util/Comparators.java b/jdk/src/share/classes/java/util/Comparators.java
new file mode 100644
index 00000000000..75821339b49
--- /dev/null
+++ b/jdk/src/share/classes/java/util/Comparators.java
@@ -0,0 +1,279 @@
+/*
+ * Copyright (c) 2012, 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. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+package java.util;
+
+import java.io.Serializable;
+import java.util.function.BinaryOperator;
+import java.util.function.Function;
+import java.util.function.ToDoubleFunction;
+import java.util.function.ToIntFunction;
+import java.util.function.ToLongFunction;
+
+/**
+ * This class consists of {@code static} utility methods for comparators. Mostly
+ * factory method that returns a {@link Comparator}.
+ *
+ *
Unless otherwise noted, passing a {@code null} argument to a method in
+ * this class will cause a {@link NullPointerException} to be thrown.
+ *
+ * @see Comparator
+ * @since 1.8
+ */
+public class Comparators {
+ private Comparators() {
+ throw new AssertionError("no instances");
+ }
+
+ /**
+ * Compares {@link Comparable} objects in natural order.
+ *
+ * @see Comparable
+ */
+ private enum NaturalOrderComparator implements Comparator> {
+ INSTANCE;
+
+ @Override
+ public int compare(Comparable