Java Platform 1.2
Beta 4

Interface java.util.Comparator

All Known Implementing Classes:
Collator

public abstract interface Comparator
A comparison function, which imposes a partial ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).

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.

Since:
JDK1.2
See Also:
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

compare

public int compare(Object o1,
                   Object o2)
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

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.

Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
Throws:
ClassCastException - the arguments' types prevent them from being compared by this Comparator.

equals

public boolean equals(Object obj)
Indicates whether some other object is "equal to" this Comparator. This method must obey the general contract of Object.equals(Object). Additionally, this method can return true only if the specified Object is also a Comparator and it imposes the same ordering as this Comparator. Thus, 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.

Parameters:
obj - the reference object with which to compare.
Returns:
true only if the specified object is also a Comparator and it imposes the same ordering as this Comparator; false otherwise.
Overrides:
equals in class Object
See Also:
Object.equals(java.lang.Object), Object.hashCode()

Java Platform 1.2
Beta 4

Submit a bug or feature
Submit comments/suggestions about new javadoc look
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.
This documentation was generated with a post-Beta4 version of Javadoc.