|
Java Platform 1.2 Beta 4 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
A Comparator c imposes a total ordering on a set of elements S if and only if c.compare(e1, e2)==0 implies that ((e1==null && e2==null) || e1.equals(e2)) for every e1 and e2 in S. A partial ordering that is not total is said to be strictly partial. This is mentioned here because some APIs that call for a Comparator (such as SortedSet and SortedMap) will behave differently depending on whether the Comparator imposes a total ordering or strictly partial.
Note: It is generally a good idea for Comparators to 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.
Comparable
,
Arrays.sort(Object[], Comparator)
,
TreeMap
,
TreeSet
,
SortedMap
,
SortedSet
,
Serializable
Method Summary | |
int | compare(Object o1,
Object o2)
Compares its two arguments for order. |
boolean | equals(Object obj)
Indicates whether some other object is "equal to" this Comparator. |
Method Detail |
public int compare(Object o1, Object o2)
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.
The implementer must also ensure that x.equals(y) || (x==null && y==null) implies that compare(x, y)==0. Note that the converse is not necessarily true. If the converse is false, this Comparator imposes a strictly partial ordering (a partial ordering that is not total).
Finally, the implementer must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.
public boolean equals(Object obj)
comp1.equals(comp2)
implies that
sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for
every object reference o1 and o2.
Note that it is always safe not to override Object.equals(Object). However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct Comparators impose the same order.
obj
- the reference object with which to compare.true
only if the specified object is also
a Comparator and it imposes the same ordering as this
Comparator; false
otherwise.Object.equals(java.lang.Object)
,
Object.hashCode()
|
Java Platform 1.2 Beta 4 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |