< prev index next >

src/java.base/share/classes/java/util/Comparator.java

Print this page

        

*** 21,38 **** * 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.Function; ! 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 some * collection of objects. Comparators can be passed to a sort method (such * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link --- 21,36 ---- * 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 javany.util; import java.io.Serializable; ! import java.util.Objects; ! ! import javany.util.function.Function; /** * A comparison function, which imposes a <i>total ordering</i> on some * collection of objects. Comparators can be passed to a sort method (such * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
*** 104,114 **** * @see Comparable * @see java.io.Serializable * @since 1.2 */ @FunctionalInterface ! public interface Comparator<T> { /** * Compares its two arguments for order. Returns a negative integer, * zero, or a positive integer as the first argument is less than, equal * to, or greater than the second.<p> * --- 102,112 ---- * @see Comparable * @see java.io.Serializable * @since 1.2 */ @FunctionalInterface ! public interface Comparator<any T> { /** * Compares its two arguments for order. Returns a negative integer, * zero, or a positive integer as the first argument is less than, equal * to, or greater than the second.<p> *
*** 180,190 **** * @return a comparator that imposes the reverse ordering of this * comparator. * @since 1.8 */ default Comparator<T> reversed() { ! return Collections.reverseOrder(this); } /** * Returns a lexicographic-order comparator with another comparator. * If this {@code Comparator} considers two elements equal, i.e. --- 178,188 ---- * @return a comparator that imposes the reverse ordering of this * comparator. * @since 1.8 */ default Comparator<T> reversed() { ! return new Comparators.ReverseComparator<>(this); } /** * Returns a lexicographic-order comparator with another comparator. * If this {@code Comparator} considers two elements equal, i.e.
*** 233,243 **** * @throws NullPointerException if either argument is null. * @see #comparing(Function, Comparator) * @see #thenComparing(Comparator) * @since 1.8 */ ! default <U> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { return thenComparing(comparing(keyExtractor, keyComparator)); } --- 231,241 ---- * @throws NullPointerException if either argument is null. * @see #comparing(Function, Comparator) * @see #thenComparing(Comparator) * @since 1.8 */ ! default <any U> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { return thenComparing(comparing(keyExtractor, keyComparator)); }
*** 274,288 **** * * @param keyExtractor the function used to extract the integer sort key * @return a lexicographic-order comparator composed of this and then the * {@code int} sort key * @throws NullPointerException if the argument is null. ! * @see #comparingInt(ToIntFunction) * @see #thenComparing(Comparator) * @since 1.8 */ ! default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) { return thenComparing(comparingInt(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that --- 272,286 ---- * * @param keyExtractor the function used to extract the integer sort key * @return a lexicographic-order comparator composed of this and then the * {@code int} sort key * @throws NullPointerException if the argument is null. ! * @see #comparingInt(Function) * @see #thenComparing(Comparator) * @since 1.8 */ ! default Comparator<T> thenComparingInt(Function<? super T, int> keyExtractor) { return thenComparing(comparingInt(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that
*** 293,307 **** * * @param keyExtractor the function used to extract the long sort key * @return a lexicographic-order comparator composed of this and then the * {@code long} sort key * @throws NullPointerException if the argument is null. ! * @see #comparingLong(ToLongFunction) * @see #thenComparing(Comparator) * @since 1.8 */ ! default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) { return thenComparing(comparingLong(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that --- 291,305 ---- * * @param keyExtractor the function used to extract the long sort key * @return a lexicographic-order comparator composed of this and then the * {@code long} sort key * @throws NullPointerException if the argument is null. ! * @see #comparingLong(Function) * @see #thenComparing(Comparator) * @since 1.8 */ ! default Comparator<T> thenComparingLong(Function<? super T, long> keyExtractor) { return thenComparing(comparingLong(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that
*** 312,326 **** * * @param keyExtractor the function used to extract the double sort key * @return a lexicographic-order comparator composed of this and then the * {@code double} sort key * @throws NullPointerException if the argument is null. ! * @see #comparingDouble(ToDoubleFunction) * @see #thenComparing(Comparator) * @since 1.8 */ ! default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) { return thenComparing(comparingDouble(keyExtractor)); } /** * Returns a comparator that imposes the reverse of the <em>natural --- 310,324 ---- * * @param keyExtractor the function used to extract the double sort key * @return a lexicographic-order comparator composed of this and then the * {@code double} sort key * @throws NullPointerException if the argument is null. ! * @see #comparingDouble(Function) * @see #thenComparing(Comparator) * @since 1.8 */ ! default Comparator<T> thenComparingDouble(Function<? super T, double> keyExtractor) { return thenComparing(comparingDouble(keyExtractor)); } /** * Returns a comparator that imposes the reverse of the <em>natural
*** 333,344 **** * @return a comparator that imposes the reverse of the <i>natural * ordering</i> on {@code Comparable} objects. * @see Comparable * @since 1.8 */ ! public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() { ! return Collections.reverseOrder(); } /** * Returns a comparator that compares {@link Comparable} objects in natural * order. --- 331,342 ---- * @return a comparator that imposes the reverse of the <i>natural * ordering</i> on {@code Comparable} objects. * @see Comparable * @since 1.8 */ ! public static <any T> Comparator<T> reverseOrder() { ! return Comparators.<T>naturalOrder().reversed(); } /** * Returns a comparator that compares {@link Comparable} objects in natural * order.
*** 351,362 **** * Comparable} objects. * @see Comparable * @since 1.8 */ @SuppressWarnings("unchecked") ! public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() { ! return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE; } /** * Returns a null-friendly comparator that considers {@code null} to be * less than non-null. When both are {@code null}, they are considered --- 349,360 ---- * Comparable} objects. * @see Comparable * @since 1.8 */ @SuppressWarnings("unchecked") ! public static <any T> Comparator<T> naturalOrder() { ! return Comparators.naturalOrder(); } /** * Returns a null-friendly comparator that considers {@code null} to be * less than non-null. When both are {@code null}, they are considered
*** 424,434 **** * @return a comparator that compares by an extracted key using the * specified {@code Comparator} * @throws NullPointerException if either argument is null * @since 1.8 */ ! public static <T, U> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator); --- 422,432 ---- * @return a comparator that compares by an extracted key using the * specified {@code Comparator} * @throws NullPointerException if either argument is null * @since 1.8 */ ! public static <any T, any U> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator);
*** 459,469 **** * Comparable} sort key * @return a comparator that compares by an extracted key * @throws NullPointerException if the argument is null * @since 1.8 */ ! public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); --- 457,467 ---- * Comparable} sort key * @return a comparator that compares by an extracted key * @throws NullPointerException if the argument is null * @since 1.8 */ ! public static <any T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
*** 482,495 **** * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ ! public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) ! (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); } /** * Accepts a function that extracts a {@code long} sort key from a type * {@code T}, and returns a {@code Comparator<T>} that compares by that --- 480,493 ---- * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ ! public static <any T> Comparator<T> comparingInt(Function<? super T, int> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) ! (c1, c2) -> Integer.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); } /** * Accepts a function that extracts a {@code long} sort key from a type * {@code T}, and returns a {@code Comparator<T>} that compares by that
*** 503,516 **** * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ ! public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) ! (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); } /** * Accepts a function that extracts a {@code double} sort key from a type * {@code T}, and returns a {@code Comparator<T>} that compares by that --- 501,514 ---- * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ ! public static <any T> Comparator<T> comparingLong(Function<? super T, long> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) ! (c1, c2) -> Long.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); } /** * Accepts a function that extracts a {@code double} sort key from a type * {@code T}, and returns a {@code Comparator<T>} that compares by that
*** 524,534 **** * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ ! public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) ! (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); } } --- 522,532 ---- * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ ! public static <any T> Comparator<T> comparingDouble(Function<? super T, double> keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator<T> & Serializable) ! (c1, c2) -> Double.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); } }
< prev index next >