Java Reference
In-Depth Information
Comparing Objects
When you have a group of objects, sometimes you may want to order them based on some criteria. The
java.lang.Comparable and java.util.Comparator are two commonly used interfaces for the purpose of ordering
objects. I will discuss both interfaces in this section.
Using the Comparable Interface
A class implements the Comparable interface if objects of the class need to be compared for sorting purposes. For
example, you may want to compare two objects of a Person class when sorting a collection of persons in an array or
a list. The criteria you use to compare the two objects depend on the context. For example, when you need to display
many persons, you may want to display them sorted by their last names, person ids, addresses, or telephone numbers.
The ordering on the objects of a class that is imposed by the Comparable interface is also called the class's natural
ordering . The Comparable interface contains an abstract compareTo() method that takes one parameter. The method
returns zero if the two objects being compared are considered equal; it returns a negative integer if the object is less
than the specified parameter; it returns a positive integer if the object is greater than the specified parameter.
The Comparable interface is a generic interface declared as follows:
public interface Comparable<T> {
public int compareTo(T o);
}
The String class and wrapper classes ( Integer , Double , Float , etc.) implement the Comparable interface. The
String class's compareTo() method sorts strings lexicographically. All wrapper classes for the numeric primitive types
compare the two objects numerically.
Before Java 5, the compareTo() method must accept only the Object type argument. However, from Java 5, you
need to use a parameter type for the Comparable interface, which indicates the type of parameter to its compareTo()
method. It is typical to compare objects of the same type. Before Java 5, you needed to perform a check inside the
compareTo() method to determine if the object being passed to the argument is of the same type or not. When you use
the generic version of the Comparable interface, the compareTo() method's parameter type can be specified as shown
below. The following class declaration for the class A implements the Comparable<A> interface using A as its generic
type, which states that the class A supports only comparing objects of its own type:
public class A implement Comparable<A> {
public int compareTo(A a) {
/* Code goes here */
}
}
Listing 17-33 contains the code for a ComparablePerson class that implements the
Comparable<ComparablePerson> interface. In the compareTo() method, first you compare the two objects based
on their last names. If the last names are the same, you compare their first names. You have used the compareTo()
method of the String class to compare the last and first names of two comparable persons. Note that the compareTo()
method does not handle null values.
 
Search WWH ::




Custom Search