Package java.util
Interface Comparator<T>
-
- All Known Implementing Classes:
AttributeTypeAndValueComparator,Collator,Collator,IterableComparator,MultiComparator,RuleBasedCollator,RuleBasedCollator,UTF16.StringComparator
public interface Comparator<T>AComparatoris used to compare two objects to determine their ordering with respect to each other. On a givenCollection, aComparatorcan be used to obtain a sortedCollectionwhich is totally ordered. For aComparatorto 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 aComparatorimplementsSerializable.- Since:
- 1.2
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description intcompare(T lhs, T rhs)Compares the two specified objects to determine their relative ordering.booleanequals(Object object)Compares thisComparatorwith the specifiedObjectand indicates whether they are equal.
-
-
-
Method Detail
-
compare
int compare(T lhs, T rhs)
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(lhs, rhs)should form an equivalence relation. This means thatcompare(a,a)returns zero for alla- the 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:
lhs- anObject.rhs- a secondObjectto compare withlhs.- Returns:
- an integer < 0 if
lhsis less thanrhs, 0 if they are equal, and > 0 iflhsis greater thanrhs. - Throws:
ClassCastException- if objects are not of the correct type.
-
equals
boolean equals(Object object)
Compares thisComparatorwith the specifiedObjectand indicates whether they are equal. In order to be equal,objectmust represent the same object as this instance using a class-specific comparison.A
Comparatornever needs to override this method, but may choose so for performance reasons.- Overrides:
equalsin classObject- Parameters:
object- theObjectto compare with this comparator.- Returns:
- boolean
trueif specifiedObjectis the same as thisObject, andfalseotherwise. - See Also:
Object.hashCode(),Object.equals(java.lang.Object)
-
-