GeezerVison is showing docs.oracle.com — Comparator (Java Platform SE 8 )
Java™ Platform Standard Ed. 8
- Summary:
- Nested |
- Field |
- Constr |
- Method
- Detail:
- Field |
- Constr |
- Method
compact1, compact2, compact3
java.util
Interface Comparator<T>
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods
| Modifier and Type |
Method |
Description |
int |
compare(T o1,
T o2) |
Compares its two arguments for order.
|
static <T,U extends Comparable<? super U>> Comparator<T> |
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.
|
boolean |
equals(Object obj) |
Indicates whether some other object is "equal to" this
comparator.
|
static <T extends Comparable<? super T>> Comparator<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.
|
default Comparator<T> |
reversed() |
Returns a comparator that imposes the reverse ordering of this
comparator.
|
static <T extends Comparable<? super T>> Comparator<T> |
reverseOrder() |
Returns a comparator that imposes the reverse of the natural
ordering.
|
default Comparator<T> |
thenComparing(Comparator<? super T> other) |
Returns a lexicographic-order comparator with another comparator.
|
default <U extends Comparable<? super U>> Comparator<T> |
thenComparing(Function<? super T,? extends U> keyExtractor) |
Returns a lexicographic-order comparator with a function that
extracts a Comparable sort key.
|
default <U> Comparator<T> |
thenComparing(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.
|
default Comparator<T> |
thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) |
Returns a lexicographic-order comparator with a function that
extracts a double sort key.
|
default Comparator<T> |
thenComparingInt(ToIntFunction<? super T> keyExtractor) |
Returns a lexicographic-order comparator with a function that
extracts a int sort key.
|
default Comparator<T> |
thenComparingLong(ToLongFunction<? super T> keyExtractor) |
Returns a lexicographic-order comparator with a function that
extracts a long sort key.
|
-
-
Method Detail
-
compare
int compare(T o1,
T o2)
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.
In the foregoing description, the notation
sgn(expression) designates the mathematical
signum function, which is defined to return one of -1,
0, or 1 according to whether the value of
expression is negative, zero or positive.
The implementor must ensure that sgn(compare(x, y)) ==
-sgn(compare(y, x)) for all x and y. (This
implies that compare(x, y) must throw an exception if and only
if compare(y, x) throws an exception.)
The implementor must also ensure that the relation is transitive:
((compare(x, y)>0) && (compare(y, z)>0)) implies
compare(x, z)>0.
Finally, the implementor must ensure that compare(x, y)==0
implies that sgn(compare(x, z))==sgn(compare(y, z)) for all
z.
It is generally the case, but not strictly required that
(compare(x, y)==0) == (x.equals(y)). Generally speaking,
any comparator that violates this condition should clearly indicate
this fact. The recommended language is "Note: this comparator
imposes orderings that are inconsistent with equals."
- Parameters:
o1 - the first object to be compared.
o2 - the second object to be compared.
- Returns:
- a negative integer, zero, or a positive integer as the
first argument is less than, equal to, or greater than the
second.
- Throws:
NullPointerException - if an argument is null and this
comparator does not permit null arguments
ClassCastException - if the arguments' types prevent them from
being compared by this comparator.
-
thenComparing
default <U> Comparator<T> thenComparing(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.
- Implementation Requirements:
- This default implementation behaves as if
thenComparing(comparing(keyExtractor, cmp)).
- Type Parameters:
U - the type of the sort key
- Parameters:
keyExtractor - the function used to extract the sort key
keyComparator - the Comparator used to compare the sort key
- Returns:
- a lexicographic-order comparator composed of this comparator
and then comparing on the key extracted by the keyExtractor function
- Throws:
NullPointerException - if either argument is null.
- Since:
- 1.8
- See Also:
comparing(Function, Comparator),
thenComparing(Comparator)
-
thenComparing
default <U extends Comparable<? super U>> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor)
Returns a lexicographic-order comparator with a function that
extracts a Comparable sort key.
- Implementation Requirements:
- This default implementation behaves as if
thenComparing(comparing(keyExtractor)).
- Type Parameters:
U - the type of the Comparable sort key
- Parameters:
keyExtractor - the function used to extract the Comparable sort key
- Returns:
- a lexicographic-order comparator composed of this and then the
Comparable sort key.
- Throws:
NullPointerException - if the argument is null.
- Since:
- 1.8
- See Also:
comparing(Function),
thenComparing(Comparator)
-
comparing
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.
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 = Comparator.comparing(
Person::getLastName,
String.CASE_INSENSITIVE_ORDER);
- Type Parameters:
T - the type of element to be compared
U - the type of the sort key
- Parameters:
keyExtractor - the function used to extract the sort key
keyComparator - the Comparator used to compare the sort key
- Returns:
- a comparator that compares by an extracted key using the
specified
Comparator
- Throws:
NullPointerException - if either argument is null
- Since:
- 1.8
Java™ Platform Standard Ed. 8
- Summary:
- Nested |
- Field |
- Constr |
- Method
- Detail:
- Field |
- Constr |
- Method
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, 2026, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
|