Interface Comparator<T>
A Comparator is used to compare two objects to determine their ordering with
respect to each other. On a given Collection, a Comparator can be used to
obtain a sorted Collection which is totally ordered. For a Comparator
to be consistent with equals, its {code #compare(Object, Object)}
method has to return zero for each pair of elements (a,b) where a.equals(b)
holds true. It is recommended that a Comparator implements
java.io.Serializable.
Since
1.2
-
Method Summary
Modifier and TypeMethodDescriptionintCompares the two specified objects to determine their relative ordering.static <T, U extends Comparable<? super U>>
Comparator<T> Compares by the natural order of a key.static <T,U> Comparator <T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) Compares by a key, using the given comparator for the keys.static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) Compares by adoublekey, asDouble.compare(double, double)does: NaN above everything and-0.0below0.0.static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) Compares by anintkey.static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) Compares by alongkey.booleanCompares thisComparatorwith the specifiedObjectand indicates whether they are equal.static <T extends Comparable<? super T>>
Comparator<T> The natural order of comparable values.static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) Orders null before every other value, and the rest bycomparator; with a nullcomparatorthe others are all equal.static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) Orders null after every other value, and the rest bycomparator; with a nullcomparatorthe others are all equal.default Comparator<T> reversed()This ordering, reversed.static <T extends Comparable<? super T>>
Comparator<T> The reverse of the natural order.default Comparator<T> thenComparing(Comparator<? super T> other) This ordering, ties broken byother.default <U extends Comparable<? super U>>
Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor) This ordering, ties broken by the natural order of a key.default <U> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) This ordering, ties broken by a key in the orderkeyComparatorgives.default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) This ordering, ties broken by adoublekey, compared asDouble.compare(double, double)does.default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) This ordering, ties broken by anintkey.default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) This ordering, ties broken by alongkey.
-
Method Details
-
compare
Compares the two specified objects to determine their relative ordering. The ordering implied by the return value of this method for all possible pairs of
(object1, object2)should form an equivalence relation. This means that-
compare(a,a)returns zero for alla -
the sign of
compare(a,b)must be the opposite of the sign ofcompare(b,a)for all pairs of (a,b) -
From
compare(a,b) > 0andcompare(b,c) > 0it must followcompare(a,c) > 0for all possible combinations of(a,b,c)
Parameters
-
object1: anObject. -
object2: a secondObjectto compare withobject1.
Returns
an integer 0 if
object1is greater thanobject2.Throws
ClassCastException: if objects are not of the correct type.
-
-
equals
Compares this
Comparatorwith the specifiedObjectand indicates whether they are equal. In order to be equal,objectmust represent the same object as this instance using a class-specific comparison.A
Comparatornever needs to override this method, but may choose so for performance reasons.Parameters
object: theObjectto compare with this comparator.
Returns
-
reversed
-
thenComparing
This ordering, ties broken byother.- Parameters:
other- the tie breaker- Returns:
- the combined comparator
-
thenComparing
default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor) This ordering, ties broken by the natural order of a key.- Type Parameters:
U- the key type- Parameters:
keyExtractor- extracts the key- Returns:
- the combined comparator
-
comparing
static <T, U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor) Compares by the natural order of a key.- Type Parameters:
T- the compared typeU- the key type- Parameters:
keyExtractor- extracts the key- Returns:
- the comparator
-
comparing
static <T,U> Comparator<T> comparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) Compares by a key, using the given comparator for the keys.- Type Parameters:
T- the compared typeU- the key type- Parameters:
keyExtractor- extracts the keykeyComparator- orders the keys- Returns:
- the comparator
-
naturalOrder
The natural order of comparable values.- Type Parameters:
T- the compared type- Returns:
- the comparator
-
reverseOrder
The reverse of the natural order.- Type Parameters:
T- the compared type- Returns:
- the comparator
-
thenComparing
default <U> Comparator<T> thenComparing(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) This ordering, ties broken by a key in the orderkeyComparatorgives.- Type Parameters:
U- the key type- Parameters:
keyExtractor- extracts the keykeyComparator- orders the keys- Returns:
- the combined comparator
-
thenComparingInt
This ordering, ties broken by anintkey.- Parameters:
keyExtractor- extracts the key- Returns:
- the combined comparator
-
thenComparingLong
This ordering, ties broken by alongkey.- Parameters:
keyExtractor- extracts the key- Returns:
- the combined comparator
-
thenComparingDouble
This ordering, ties broken by adoublekey, compared asDouble.compare(double, double)does.- Parameters:
keyExtractor- extracts the key- Returns:
- the combined comparator
-
comparingInt
Compares by anintkey.- Type Parameters:
T- the compared type- Parameters:
keyExtractor- extracts the key- Returns:
- the comparator
-
comparingLong
Compares by alongkey.- Type Parameters:
T- the compared type- Parameters:
keyExtractor- extracts the key- Returns:
- the comparator
-
comparingDouble
Compares by adoublekey, asDouble.compare(double, double)does: NaN above everything and-0.0below0.0.- Type Parameters:
T- the compared type- Parameters:
keyExtractor- extracts the key- Returns:
- the comparator
-
nullsFirst
Orders null before every other value, and the rest bycomparator; with a nullcomparatorthe others are all equal.- Type Parameters:
T- the compared type- Parameters:
comparator- orders the values that are not null, or null- Returns:
- the comparator
-
nullsLast
Orders null after every other value, and the rest bycomparator; with a nullcomparatorthe others are all equal.- Type Parameters:
T- the compared type- Parameters:
comparator- orders the values that are not null, or null- Returns:
- the comparator
-