Overview | Package | Class | Tree | Index | Help | |||
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.equals(e2) || (e1==null e2==null)) for every e1 and e2 in S. This is mentioned here because some applications that call for a comparator will require that the comparator impose a total ordering (such as SortedMap and SortedSet).
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 be Serializable.
Method Summary | |
int | compare(java.lang.Object o1,
java.lang.Object o2)
Compares its two arguments for order. |
Method Detail |
public int compare(java.lang.Object o1, java.lang.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 induces a partial (not total) ordering.
Finally, the implementer must ensure that compare(x, y) == 0 implies that sgn(compare(x, z)) == sgn(compare(y, z)), for all z.
Overview | Package | Class | Tree | Index | Help | |||
PREV CLASS | NEXT CLASS | FRAMES | NO FRAMES | ||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |