Collections.sort
or Arrays.sort
) to allow precise control over the sort order.
Comparators can also be used to control the order of certain data
structures (such as sorted sets or
sorted maps), or to provide an ordering for
collections of objects that don't have a natural ordering.
The ordering imposed by a comparator c
on a set of elements
S
is said to be consistent with equals if and only if
c.compare(e1, e2)==0
has the same boolean value as
e1.equals(e2)
for every e1
and e2
in
S
.
Caution should be exercised when using a comparator capable of imposing an
ordering inconsistent with equals to order a sorted set (or sorted map).
Suppose a sorted set (or sorted map) with an explicit comparator c
is used with elements (or keys) drawn from a set S
. If the
ordering imposed by c
on S
is inconsistent with equals,
the sorted set (or sorted map) will behave "strangely." In particular the
sorted set (or sorted map) will violate the general contract for set (or
map), which is defined in terms of equals
.
For example, suppose one adds two elements a
and b
such that
(a.equals(b) && c.compare(a, b) != 0)
to an empty TreeSet
with comparator c
.
The second add
operation will return
true (and the size of the tree set will increase) because a
and
b
are not equivalent from the tree set's perspective, even though
this is contrary to the specification of the
Set.
method.
Note
It is generally a good idea for comparators to also implement
java.io.Serializable
, as they may be used as ordering methods in
serializable data structures (like TreeSet
, TreeMap
). In
order for the data structure to serialize successfully, the comparator (if
provided) must implement Serializable
.
For the mathematically inclined, the relation that defines the
imposed ordering that a given comparator c
imposes on a
given set of objects S
is:
{(x, y) such that c.compare(x, y) <= 0}.The quotient for this total order is:
{(x, y) such that c.compare(x, y) == 0}.It follows immediately from the contract for
compare
that the
quotient is an equivalence relation on S
, and that the
imposed ordering is a total order on S
. When we say that
the ordering imposed by c
on S
is consistent with
equals, we mean that the quotient for the ordering is the equivalence
relation defined by the objects' equals(Object)
method(s):{(x, y) such that x.equals(y)}.In other words, when the imposed ordering is consistent with equals, the equivalence classes defined by the equivalence relation of the
equals
method and the equivalence classes defined by
the quotient of the compare
method are the same.
Unlike Comparable
, a comparator may optionally permit
comparison of null arguments, while maintaining the requirements for
an equivalence relation.
This interface is a member of the Java Collections Framework.
Comparable
, java.io.Serializable
Modifier and Type | Method and Description |
---|---|
public int | Returns: a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.the first object to be compared. o1, T the second object to be compared. o2)Compares its two arguments for order. |
public static < the type of element to be compared T, the type of the sort key U> Comparator | Returns: a comparator that compares by an extracted key using the specifiedComparator the function used to extract the sort key keyExtractor, Comparator<? super U> the keyComparator)Comparator used to compare the sort keyAccepts a function that extracts a sort key from a type |
public static < the type of element to be compared T, the type of the U extends ComparableComparable sort key | Returns: a comparator that compares by an extracted keythe function used to extract the keyExtractor)Comparable sort keyAccepts a function that extracts a |
public static < the type of element to be compared T> Comparator | Returns: a comparator that compares by an extracted keythe function used to extract the double sort key keyExtractor)Accepts a function that extracts a |
public static < the type of element to be compared T> Comparator | Returns: a comparator that compares by an extracted keythe function used to extract the integer sort key keyExtractor)Accepts a function that extracts an |
public static < the type of element to be compared T> Comparator | Returns: a comparator that compares by an extracted keythe function used to extract the long sort key keyExtractor)Accepts a function that extracts a |
public boolean | |
public static < the T extends ComparableComparable type of element to be compared | Returns: a comparator that imposes the natural ordering onComparable objects.Returns a comparator that compares |
public static < the type of the elements to be compared T> Comparator | Returns: a comparator that considersnull to be less than
non-null, and compares non-null objects with the supplied
Comparator .a comparator)Comparator for comparing non-null valuesReturns a null-friendly comparator that considers |
public static < the type of the elements to be compared T> Comparator | Returns: a comparator that considersnull to be greater than
non-null, and compares non-null objects with the supplied
Comparator .a comparator)Comparator for comparing non-null valuesReturns a null-friendly comparator that considers |
public default Comparator | Returns: a comparator that imposes the reverse ordering of this comparator.Returns a comparator that imposes the reverse ordering of this comparator. |
public static < the T extends ComparableComparable type of element to be compared | Returns: a comparator that imposes the reverse of the natural ordering onComparable objects.Returns a comparator that imposes the reverse of the natural ordering. |
public default Comparator | Returns: a lexicographic-order comparator composed of this and then the other comparatorthe other comparator to be used when this comparator
compares two objects that are equal. other)Returns a lexicographic-order comparator with another comparator. |
public default < the type of the sort key U> Comparator | Returns: a lexicographic-order comparator composed of this comparator and then comparing on the key extracted by the keyExtractor functionthe function used to extract the sort key keyExtractor, Comparator<? super U> the keyComparator)Comparator used to compare the sort keyReturns a lexicographic-order comparator with a function that
extracts a key to be compared with the given |
public default < the type of the U extends ComparableComparable sort key | Returns: a lexicographic-order comparator composed of this and then theComparable sort key.the function used to extract the keyExtractor)Comparable sort keyReturns a lexicographic-order comparator with a function that
extracts a |
public default Comparator | Returns: a lexicographic-order comparator composed of this and then thedouble sort keythe function used to extract the double sort key keyExtractor)Returns a lexicographic-order comparator with a function that
extracts a |
public default Comparator | Returns: a lexicographic-order comparator composed of this and then theint sort keythe function used to extract the integer sort key keyExtractor)Returns a lexicographic-order comparator with a function that
extracts an |
public default Comparator | Returns: a lexicographic-order comparator composed of this and then thelong sort keythe function used to extract the long sort key keyExtractor)Returns a lexicographic-order comparator with a function that
extracts a |
compare | back to summary |
---|---|
public 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.
The implementor must ensure that
The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that API Note It is generally the case, but not strictly required that
|
comparing | back to summary |
---|---|
public static <T, U> Comparator Accepts a function that extracts a sort key from a type The returned comparator is serializable if the specified function and comparator are both serializable. API Note For example, to obtain a
|
comparing | back to summary |
---|---|
public static <T, U extends Comparable Accepts a function that extracts a The returned comparator is serializable if the specified function is also serializable. API Note For example, to obtain a
|
comparingDouble | back to summary |
---|---|
public static <T> Comparator Accepts a function that extracts a The returned comparator is serializable if the specified function is also serializable.
|
comparingInt | back to summary |
---|---|
public static <T> Comparator Accepts a function that extracts an The returned comparator is serializable if the specified function is also serializable.
|
comparingLong | back to summary |
---|---|
public static <T> Comparator Accepts a function that extracts a The returned comparator is serializable if the specified function is also serializable.
|
equals | back to summary |
---|---|
public boolean equals(Object obj) Indicates whether some other object is "equal to"
this comparator. This method must obey the general contract of
Note that it is always safe not to override
|
naturalOrder | back to summary |
---|---|
public static <T extends Comparable Returns a comparator that compares The returned comparator is serializable and throws
|
nullsFirst | back to summary |
---|---|
public static <T> Comparator Returns a null-friendly comparator that considers The returned comparator is serializable if the specified comparator is serializable.
|
nullsLast | back to summary |
---|---|
public static <T> Comparator Returns a null-friendly comparator that considers The returned comparator is serializable if the specified comparator is serializable.
|
reversed | back to summary |
---|---|
public default Comparator Returns a comparator that imposes the reverse ordering of this comparator.
|
reverseOrder | back to summary |
---|---|
public static <T extends Comparable Returns a comparator that imposes the reverse of the natural ordering. The returned comparator is serializable and throws
|
thenComparing | back to summary |
---|---|
public default Comparator Returns a lexicographic-order comparator with another comparator.
If this The returned comparator is serializable if the specified comparator is also serializable. API Note For example, to sort a collection of
|
thenComparing | back to summary |
---|---|
public default <U> Comparator Returns a lexicographic-order comparator with a function that
extracts a key to be compared with the given Implementation Specification This default implementation behaves as if
|
thenComparing | back to summary |
---|---|
public default <U extends Comparable Returns a lexicographic-order comparator with a function that
extracts a Implementation Specification This default implementation behaves as if
|
thenComparingDouble | back to summary |
---|---|
public default Comparator Returns a lexicographic-order comparator with a function that
extracts a Implementation Specification This default implementation behaves as if
|
thenComparingInt | back to summary |
---|---|
public default Comparator Returns a lexicographic-order comparator with a function that
extracts an Implementation Specification This default implementation behaves as if
|
thenComparingLong | back to summary |
---|---|
public default Comparator Returns a lexicographic-order comparator with a function that
extracts a Implementation Specification This default implementation behaves as if
|