Package Summary Overview Summary |
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
Comparator s. |
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
type Comparable
interface. Comparable
public 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 compared cmp
- a comparator whose ordering is to be reversed by the returned comparator public static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
Comparable
type in natural order. T
- Comparable
type public static <K extends Comparable<? super K>,V> Comparator<Map.Entry<K,V>> naturalOrderKeys()
Map.Entry
in natural order on key. K
- Comparable
key type V
- value type public static <K,V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> naturalOrderValues()
Map.Entry
in natural order on value. K
- key type V
- Comparable
value type public 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 type V
- value type cmp
- the key Comparator
public static <K,V> Comparator<Map.Entry<K,V>> byValue(Comparator<? super V> cmp)
Map.Entry
by value using the given Comparator
. K
- key type V
- value type cmp
- the value Comparator
public 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 type U
- the Comparable
type for comparison keyExtractor
- the function used to extract the Comparable
sort key public 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 type keyExtractor
- the function used to extract the integer value comparing(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 type keyExtractor
- the function used to extract the long value comparing(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 type keyExtractor
- the function used to extract the double value comparing(Function)
public static <T> Comparator<T> compose(Comparator<? super T> first, Comparator<? super T> second)
Comparator
s. 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 compared first
- the first comparator second
- the secondary comparator used when equals on the first public static <T> BinaryOperator<T> lesserOf(Comparator<? super T> comparator)
BinaryOperator
which returns the lesser of two elements according to the specified Comparator
T
- the type of the elements to be compared comparator
- A Comparator
for comparing the two values BinaryOperator
which returns the lesser of its operands, according to the supplied Comparator
public static <T> BinaryOperator<T> greaterOf(Comparator<? super T> comparator)
BinaryOperator
which returns the greater of two elements according to the specified Comparator
T
- the type of the elements to be compared comparator
- A Comparator
for comparing the two values BinaryOperator
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-b00