public final class Comparators extends Object
Comparator
interface.Modifier and Type | Method and Description |
---|---|
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,U> Comparator<T> |
comparing(Function<? super T,? extends U> keyExtractor,
Comparator<? super U> keyComparator)
Accepts a function that extracts a sort key from a type
T , and
returns a Comparator<T> that compares by that sort key using
the specified Comparator . |
static <T> Comparator<T> |
comparingDouble(ToDoubleFunction<? super T> keyExtractor)
Accepts a function that extracts a
double sort key from a type
T , and returns a Comparator<T> that compares by that
sort key. |
static <T> Comparator<T> |
comparingInt(ToIntFunction<? super T> keyExtractor)
Accepts a function that extracts an
int sort key from a type
T , and returns a Comparator<T> that compares by that
sort key. |
static <T> Comparator<T> |
comparingLong(ToLongFunction<? super T> keyExtractor)
Accepts a function that extracts a
long sort key from a type
T , and returns a Comparator<T> that compares by that
sort key. |
static <T extends Comparable<? super T>> |
naturalOrder()
Returns a comparator that compares
Comparable objects in natural
order. |
static <T> Comparator<T> |
nullsFirst(Comparator<? super T> comparator)
Returns a null-friendly comparator that considers
null to be
less than non-null. |
static <T> Comparator<T> |
nullsLast(Comparator<? super T> comparator)
Returns a null-friendly comparator that considers
null to be
greater than non-null. |
static <T> Comparator<T> |
reversed(Comparator<T> comparator)
Returns a comparator that imposes the reverse ordering of the
passed
comparator . |
static <T extends Comparable<? super T>> |
reverseOrder()
Returns a comparator that imposes the reverse of the natural
ordering.
|
static <T> Comparator<T> |
thenComparing(Comparator<? super T> this_,
Comparator<? super T> other)
Returns a lexicographic-order comparator with another comparator.
|
static <T,U extends Comparable<? super U>> |
thenComparing(Comparator<? super T> this_,
Function<? super T,? extends U> keyExtractor)
Returns a lexicographic-order comparator with a function that
extracts a
Comparable sort key. |
static <T,U> Comparator<T> |
thenComparing(Comparator<? super T> this_,
Function<? super T,? extends U> keyExtractor,
Comparator<? super U> keyComparator)
Returns a lexicographic-order comparator with a function that
extracts a key to be compared with the given
Comparator . |
static <T> Comparator<T> |
thenComparingDouble(Comparator<? super T> this_,
ToDoubleFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that
extracts a
double sort key. |
static <T> Comparator<T> |
thenComparingInt(Comparator<? super T> this_,
ToIntFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that
extracts a
int sort key. |
static <T> Comparator<T> |
thenComparingLong(Comparator<? super T> this_,
ToLongFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that
extracts a
long sort key. |
public static <T extends Comparable<? super T>> Comparator<T> reverseOrder()
The returned comparator is serializable and throws NullPointerException
when comparing null
.
T
- the Comparable
type of element to be comparedComparable
objects.Comparable
public static <T extends Comparable<? super T>> Comparator<T> naturalOrder()
Comparable
objects in natural
order.
The returned comparator is serializable and throws NullPointerException
when comparing null
.
T
- the Comparable
type of element to be comparedComparable
objects.Comparable
public static <T,U> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
T
, and
returns a Comparator<T>
that compares by that sort key using
the specified Comparator
.
The returned comparator is serializable if the specified function and comparator are both serializable.
API Note:
For example, to obtain a Comparator
that compares Person
objects by their last name ignoring case differences,
Comparator<Person> cmp = Comparators.comparing(
Person::getLastName,
String.CASE_INSENSITIVE_ORDER);
T
- the type of element to be comparedU
- the type of the sort keykeyExtractor
- the function used to extract the sort keykeyComparator
- the Comparator
used to compare the sort keyComparator
NullPointerException
- if either argument is nullpublic 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.
The returned comparator is serializable if the specified function is also serializable.
API Note:
For example, to obtain a Comparator
that compares Person
objects by their last name,
Comparator<Person> byLastName = Comparators.comparing(Person::getLastName);
T
- the type of element to be comparedU
- the type of the Comparable
sort keykeyExtractor
- the function used to extract the Comparable
sort keyNullPointerException
- if the argument is nullpublic static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)
int
sort key from a type
T
, and returns a Comparator<T>
that compares by that
sort key.
The returned comparator is serializable if the specified function is also serializable.
T
- the type of element to be comparedkeyExtractor
- the function used to extract the integer sort keyNullPointerException
- if the argument is nullcomparing(Function)
public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)
long
sort key from a type
T
, and returns a Comparator<T>
that compares by that
sort key.
The returned comparator is serializable if the specified function is also serializable.
T
- the type of element to be comparedkeyExtractor
- the function used to extract the long sort keyNullPointerException
- if the argument is nullcomparing(Function)
public static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor)
double
sort key from a type
T
, and returns a Comparator<T>
that compares by that
sort key.
The returned comparator is serializable if the specified function is also serializable.
T
- the type of element to be comparedkeyExtractor
- the function used to extract the double sort keyNullPointerException
- if the argument is nullcomparing(Function)
public static <T> Comparator<T> thenComparing(Comparator<? super T> this_, Comparator<? super T> other)
this_
Comparator
considers two elements equal, i.e.
compare(a, b) == 0
, other
is used to determine the order.
The returned comparator is serializable if the specified comparators are also serializable.
API Note:
For example, to sort a collection of String
based on the length
and then case-insensitive natural ordering, the comparators can be
composed using following code,
Comparator<String> cmp = Comparators.thenComparing(Comparators.comparingInt(String::length),
String.CASE_INSENSITIVE_ORDER);
T
- the type of objects that may be compared by the passed comparatorsthis_
- the comparator to be used firstother
- the other comparator to be used when the this_
comparator
compares two objects that are equal.this_
and then the
other comparatorNullPointerException
- if this_
is null.NullPointerException
- if other
is null.public static <T,U> Comparator<T> thenComparing(Comparator<? super T> this_, Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
Comparator
.
Implementation Requirements:
This default implementation behaves as if thenComparing(this_, comparing(keyExtractor, keyComparator))
.
T
- the type of objects that may be compared by the this_
comparatorU
- the type of the sort keythis_
- the comparator to be used firstkeyExtractor
- the function used to extract the sort keykeyComparator
- the Comparator
used to compare the sort keythis_
comparator
and then comparing on the key extracted by the keyExtractor functionNullPointerException
- if either argument is null.comparing(Function, Comparator)
,
thenComparing(Comparator, Comparator)
public static <T,U extends Comparable<? super U>> Comparator<T> thenComparing(Comparator<? super T> this_, Function<? super T,? extends U> keyExtractor)
Comparable
sort key.
Implementation Requirements:
This default implementation behaves as if thenComparing(this_, comparing(keyExtractor))
.
T
- the type of objects that may be compared by the this_
comparatorU
- the type of the Comparable
sort keythis_
- the comparator to be used firstkeyExtractor
- the function used to extract the Comparable
sort keythis_
and then the
Comparable
sort key.NullPointerException
- if either argument is null.comparing(Function)
,
thenComparing(Comparator, Comparator)
public static <T> Comparator<T> thenComparingInt(Comparator<? super T> this_, ToIntFunction<? super T> keyExtractor)
int
sort key.
Implementation Requirements:
This default implementation behaves as if thenComparing(this_, comparingInt(keyExtractor))
.
T
- the type of objects that may be compared by the this_
comparatorthis_
- the comparator to be used firstkeyExtractor
- the function used to extract the integer sort keythis_
and then the
int
sort keyNullPointerException
- if either argument is null.comparingInt(ToIntFunction)
,
thenComparing(Comparator, Comparator)
public static <T> Comparator<T> thenComparingLong(Comparator<? super T> this_, ToLongFunction<? super T> keyExtractor)
long
sort key.
Implementation Requirements:
This default implementation behaves as if thenComparing(this_, comparingLong(keyExtractor))
.
T
- the type of objects that may be compared by the this_
comparatorthis_
- the comparator to be used firstkeyExtractor
- the function used to extract the long sort keythis_
and then the
long
sort keyNullPointerException
- if either argument is null.comparingLong(ToLongFunction)
,
thenComparing(Comparator, Comparator)
public static <T> Comparator<T> thenComparingDouble(Comparator<? super T> this_, ToDoubleFunction<? super T> keyExtractor)
double
sort key.
Implementation Requirements:
This default implementation behaves as if thenComparing(this_, comparingDouble(keyExtractor))
.
T
- the type of objects that may be compared by the this_
comparatorthis_
- the comparator to be used firstkeyExtractor
- the function used to extract the double sort keythis_
and then the
double
sort keyNullPointerException
- if either argument is null.comparingDouble(ToDoubleFunction)
,
thenComparing(Comparator, Comparator)
public static <T> Comparator<T> reversed(Comparator<T> comparator)
comparator
.T
- the type of objects that may be compared by the comparator argumentcomparator
- the comparator whose ordering needs to be reversedcomparator
.public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
null
to be
less than non-null. When both are null
, they are considered
equal. If both are non-null, the specified Comparator
is used
to determine the order. If the specified comparator is null
,
then the returned comparator considers all non-null values to be equal.
The returned comparator is serializable if the specified comparator is serializable.
T
- the type of the elements to be comparedcomparator
- a Comparator
for comparing non-null valuesnull
to be less than
non-null, and compares non-null objects with the supplied
Comparator
.public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
null
to be
greater than non-null. When both are null
, they are considered
equal. If both are non-null, the specified Comparator
is used
to determine the order. If the specified comparator is null
,
then the returned comparator considers all non-null values to be equal.
The returned comparator is serializable if the specified comparator is serializable.
T
- the type of the elements to be comparedcomparator
- a Comparator
for comparing non-null valuesnull
to be greater than
non-null, and compares non-null objects with the supplied
Comparator
.Copyright © 2017. All rights reserved.