public class Comparators extends Object
static utility methods for comparators. Mostly
factory method that returns a Comparator.
Unless otherwise noted, passing a null argument to a method in
this class will cause a NullPointerException to be thrown.
Comparator| Modifier and Type | Method and Description |
|---|---|
static <K,V> Comparator<Map.Entry<K,V>> |
byKey(Comparator<? super K> cmp)
Gets a comparator compares
Map.Entry by key using the given
Comparator. |
static <K,V> Comparator<Map.Entry<K,V>> |
byValue(Comparator<? super V> cmp)
Gets a comparator compares
Map.Entry by value using the given
Comparator. |
static <T,U extends Comparable<? super U>> |
comparing(Function<? super T,? extends U> keyExtractor)
Accepts a function that extracts a
Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key. |
static <T> Comparator<T> |
comparing(ToDoubleFunction<? super T> keyExtractor)
Accepts a function that extracts a
double value from a type
T, and returns a Comparator<T> that compares by that
value. |
static <T> Comparator<T> |
comparing(ToIntFunction<? super T> keyExtractor)
Accepts a function that extracts an
int value from a type T, and returns a Comparator<T> that compares by that value. |
static <T> Comparator<T> |
comparing(ToLongFunction<? super T> keyExtractor)
Accepts a function that extracts a
long value from a type T, and returns a Comparator<T> that compares by that value. |
static <T> Comparator<T> |
compose(Comparator<? super T> first,
Comparator<? super T> second)
Constructs a lexicographic order from two
Comparators. |
static <T> BinaryOperator<T> |
greaterOf(Comparator<? super T> comparator)
Constructs a
BinaryOperator which returns the greater of two elements
according to the specified Comparator |
static <T> BinaryOperator<T> |
lesserOf(Comparator<? super T> comparator)
Constructs a
BinaryOperator which returns the lesser of two elements
according to the specified Comparator |
static <T extends Comparable<? super T>> |
naturalOrder()
Gets a comparator compares
Comparable type in natural order. |
static <K extends Comparable<? super K>,V> |
naturalOrderKeys()
Gets a comparator compares
Map.Entry in natural order on key. |
static <K,V extends Comparable<? super V>> |
naturalOrderValues()
Gets a comparator compares
Map.Entry in natural order on value. |
static <T extends Comparable<? super T>> |
reverseOrder()
Returns a comparator that imposes the reverse of the natural
ordering.
|
static <T> Comparator<T> |
reverseOrder(Comparator<T> cmp)
Returns a comparator that imposes the reverse ordering of the specified
Comparator. |
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder()
The returned comparator is serializable.
T - Comparable typeComparable interface.Comparablepublic static <T> Comparator<T> reverseOrder(Comparator<T> cmp)
Comparator.
The returned comparator is serializable (assuming the specified comparator is also serializable).
T - the element type to be comparedcmp - a comparator whose ordering is to be reversed by the returned
comparatorpublic static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
Comparable type in natural order.T - Comparable typepublic static <K extends Comparable<? super K>,V> Comparator<Map.Entry<K,V>> naturalOrderKeys()
Map.Entry in natural order on key.K - Comparable key typeV - value typepublic static <K,V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> naturalOrderValues()
Map.Entry in natural order on value.K - key typeV - Comparable value typepublic static <K,V> Comparator<Map.Entry<K,V>> byKey(Comparator<? super K> cmp)
Map.Entry by key using the given
Comparator.
The returned comparator is serializable assuming the specified comparators are also serializable.
K - key typeV - value typecmp - the key Comparatorpublic static <K,V> Comparator<Map.Entry<K,V>> byValue(Comparator<? super V> cmp)
Map.Entry by value using the given
Comparator.K - key typeV - value typecmp - the value Comparatorpublic static <T,U extends Comparable<? super U>> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor)
Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key. For example, if a class
Person has a String-valued getter getLastName,
then comparing(Person::getLastName) would return a Comparator<Person> that compares Person objects by their last
name.T - the original element typeU - the Comparable type for comparisonkeyExtractor - the function used to extract the Comparable sort keypublic static <T> Comparator<T> comparing(ToIntFunction<? super T> keyExtractor)
int value from a type T, and returns a Comparator<T> that compares by that value.
The returned comparator is serializable assuming the specified function is also serializable.
T - the original element typekeyExtractor - the function used to extract the integer valuecomparing(Function)public static <T> Comparator<T> comparing(ToLongFunction<? super T> keyExtractor)
long value from a type T, and returns a Comparator<T> that compares by that value.
The returned comparator is serializable assuming the specified function is also serializable.
T - the original element typekeyExtractor - the function used to extract the long valuecomparing(Function)public static <T> Comparator<T> comparing(ToDoubleFunction<? super T> keyExtractor)
double value from a type
T, and returns a Comparator<T> that compares by that
value.
The returned comparator is serializable assuming the specified function is also serializable.
T - the original element typekeyExtractor - the function used to extract the double valuecomparing(Function)public static <T> Comparator<T> compose(Comparator<? super T> first, Comparator<? super T> second)
Comparators. For
example, if you have comparators byLastName and byFirstName, each of type Comparator<Person>, then compose(byLastName, byFirstName) creates a Comparator<Person>
which sorts by last name, and for equal last names sorts by first name.
The returned comparator is serializable assuming the specified comparators are also serializable.
T - the element type to be comparedfirst - the first comparatorsecond - the secondary comparator used when equals on the firstpublic static <T> BinaryOperator<T> lesserOf(Comparator<? super T> comparator)
BinaryOperator which returns the lesser of two elements
according to the specified ComparatorT - the type of the elements to be comparedcomparator - A Comparator for comparing the two valuesBinaryOperator which returns the lesser of its operands,
according to the supplied Comparatorpublic static <T> BinaryOperator<T> greaterOf(Comparator<? super T> comparator)
BinaryOperator which returns the greater of two elements
according to the specified ComparatorT - the type of the elements to be comparedcomparator - A Comparator for comparing the two valuesBinaryOperator which returns the greater of its operands,
according to the supplied Comparator Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2013, Oracle and/or its affiliates. All rights reserved.
DRAFT internal-0