The Comparable Interface
Paul Ngugi
Posted on June 17, 2024
The Comparable interface defines the compareTo method for comparing objects. Suppose you want to design a generic method to find the larger of two objects of the same type, such as two students, two dates, two circles, two rectangles, or two squares. In order to accomplish this, the two objects must be comparable, so the common behavior for the objects
must be comparable. Java provides the Comparable interface for this purpose. The interface is defined as follows:
// Interface for comparing objects, defined in java.lang
package java.lang;
public interface Comparable<E> {
public int compareTo(E o);
}
The compareTo method determines the order of this object with the specified object o and returns a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than o.
The Comparable interface is a generic interface. The generic type E is replaced by a concrete type when implementing this interface. Many classes in the Java library implement Comparable to define a natural order for objects. The classes Byte, Short, Integer, Long, Float, Double, Character, BigInteger, BigDecimal, Calendar, String, and Date all implement the Comparable interface. For example, the Integer, BigInteger, String, and Date classes are defined as follows in the Java API:
Thus, numbers are comparable, strings are comparable, and so are dates. You can use the compareTo method to compare two numbers, two strings, and two dates. For example, the following code
1 System.out.println(new Integer(3).compareTo(new Integer(5)));
2 System.out.println("ABC".compareTo("ABE"));
3 java.util.Date date1 = new java.util.Date(2013, 1, 1);
4 java.util.Date date2 = new java.util.Date(2012, 1, 1);
5 System.out.println(date1.compareTo(date2));
displays
-1
-2
1
Line 1 displays a negative value since 3 is less than 5. Line 2 displays a negative value since ABC is less than ABE. Line 5 displays a positive value since date1 is greater than date2.
Let n be an Integer object, s be a String object, and d be a Date object. All the following expressions are true.
Since all Comparable objects have the compareTo method, the java.util.Arrays.sort(Object[]) method in the Java API uses the compareTo method to compare and sorts the objects in an array, provided that the objects are instances of the Comparable interface. the program below gives an example of sorting an array of strings and an array of BigInteger objects.
The program creates an array of strings (line 7) and invokes the sort method to sort the strings (line 8). The program creates an array of BigInteger objects (lines 13) and invokes the sort method to sort the BigInteger objects (line 14).
You cannot use the sort method to sort an array of Rectangle objects, because Rectangle does not implement Comparable. However, you can define a new rectangle class that implements Comparable. The instances of this new class are comparable. Let this new class be named ComparableRectangle, as shown in the program below.
ComparableRectangle extends Rectangle and implements Comparable, as shown in Figure below. The keyword implements indicates that ComparableRectangle inherits all the constants from the Comparable interface and implements the methods in the interface. The compareTo method compares the areas of two rectangles. An instance of ComparableRectangle is also an instance of Rectangle, GeometricObject, Object, and Comparable.
You can now use the sort method to sort an array of ComparableRectangle objects, as in the program below.
Width: 3.4 Height: 5.4 Area: 18.36
Width: 1.4 Height: 25.4 Area: 35.559999999999995
Width: 7.4 Height: 35.4 Area: 261.96
Width: 13.24 Height: 55.4 Area: 733.496
An interface provides another form of generic programming. It would be difficult to use a generic sort method to sort the objects without using an interface in this example, because multiple inheritance would be necessary to inherit Comparable and another class, such as Rectangle, at the same time.
The Object class contains the equals method, which is intended for the subclasses of the Object class to override in order to compare whether the contents of the objects are the same. Suppose that the Object class contains the compareTo method, as defined in the Comparable interface; the sort method can be used to compare a list of any objects. Whether a compareTo method should be included in the Object class is debatable. Since the compareTo method is not defined in the Object class, the Comparable interface is defined in Java to enable objects to be compared if they are instances of the Comparable interface. It is strongly recommended (though not required) that compareTo should be consistent with equals. That is, for two objects o1 and o2, o1.compareTo(o2) == 0 if and only if o1.equals(o2) is true.
Posted on June 17, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.