public interface Comparator<T>
Known subtypesCaseInsensitiveOrder
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
java.io.Serializable.Methods
public abstract int compare(T object1, T object2) | Compares the two specified objects to determine their relative ordering. |
public abstract boolean equals(Object object) | Compares this Comparator with the specified Object and indicates whether they are equal. |
Method details
compare
public abstract int compare(T object1, T object2)Compares the two specified objects to determine their relative ordering. The ordering
implied by the return value of this method for all possible pairs of
(object1, object2) should form an equivalence relation.
This means that
compare(a,a)returns zero for allathe sign of
compare(a,b)must be the opposite of the sign ofcompare(b,a)for all pairs of (a,b)From
compare(a,b) > 0andcompare(b,c) > 0it must followcompare(a,c) > 0for all possible combinations of(a,b,c)
Parameters
object1T- an
Object. object2T- a second
Objectto compare withobject1.
Returns
object1 is greater than object2.Throws
ClassCastException- if objects are not of the correct type.
equals
public abstract boolean equals(Object object)Compares this Comparator with the specified Object and indicates whether they
are equal. In order to be equal, object must represent the same object
as this instance using a class-specific comparison.
A Comparator never needs to override this method, but may choose so for
performance reasons.
Parameters
objectObject- the
Objectto compare with this comparator.
Returns
true if specified Object is the same as this
Object, and false otherwise.