@FunctionalInterface
public interface Comparator<T>
Comparator
is used to compare two objects to determine their ordering with
respect to each other. On a given Collection
, a Comparator
can be used to
obtain a sorted Collection
which is totally ordered. For a Comparator
to be consistent with equals, its {code #compare(Object, Object)}
method has to return zero for each pair of elements (a,b) where a.equals(b)
holds true. It is recommended that a Comparator
implements
Serializable
.Modifier and Type | Method and Description |
---|---|
int |
compare(T object1,
T object2)
Compares the two specified objects to determine their relative ordering.
|
int compare(T object1, T object2)
(object1, object2)
should form an equivalence relation.
This means that
compare(a,a)
returns zero for all a
compare(a,b)
must be the opposite of the sign of compare(b,a)
for all pairs of (a,b)compare(a,b) > 0
and compare(b,c) > 0
it must
follow compare(a,c) > 0
for all possible combinations of (a,b,c)
object1
- an Object
.object2
- a second Object
to compare with object1
.object1
is less than object2
, 0 if they are
equal, and > 0 if object1
is greater than object2
.java.lang.ClassCastException
- if objects are not of the correct type.