Comparable interface is one of the key concepts of Java Collection Framework. We will hear about the terms "Natural Ordering" or "Natural comparison method" a lot through out the Collection framework, this chapter explains these terms.

Things To Remember
Ordering the elements based on the rules imposed by Comparable interface is called "Natural ordering"

The Comparable interface helps in ordering the object/elements. Any class that implements this interface can get its objects sorted. For example, all the elements in the SortedSet are sorted in Natural ordering. Ordering the elements based on the rules imposed by Comparable interface is called "Natural ordering". To understand more about SortedSet, please refer to Sorted Set Interface.

There is a long list of classes that implement Comparable Interface. Comparable interface defines a method "compareTo", that holds the logic for defining a natural ordering.

Methods defined in Comparable interface

Comparable interface defines just one method "int compareTo(Object o)". This method returns either "Zero" or "-ve number" or a "+ve number". Let us explain the behavior of this method using an example.

The statement "e1.compareTo(e2)" return 0 if e1 = e2. The statement "e1.compareTo(e2)" return -1 if e1 < e2. The statement "e1.compareTo(e2)" return +1 if e1 > e2.

Comparable interface is also a part of Java Collections, Now, in that context, a Set containing a list of numbers would be sorted in Ascending order. Similarly, a Set containing string would be sorted in alphabetic order. This implies, these orders would be the default/Natural Ordering. When we talk about the compareTo method, the basic definition of this method never changes, it always returns -ve or 0 or +ve. Depending upon the Class that implements it, for instance TreeSet which is an implementation of a SortedSet interface does not allow duplicate elements, it uses compareTo operation and makes sure the property of a Set is maintained. There are few other constrains like this, more details available in Sorted Set Interface and Tree Set Class.

Throughout this page, comparable interface is discussed in the context of Interfaces/classes that are part of Java Collection Framework. Here is an example program that demonstrates the standard property/usage of the "compareTo" method.

 public class ComparableDemo {
     public static void main(String[] args) {
 	String s1 = "Mike", s2 = "Tom", s3 = "Mike", s4 = "Andy";
 	Integer e1 = 5, e2 =7, e3 = 5, e4 = 1;	
 	int result;

 	result = s1.compareTo(s2);
 	System.out.println(" String Comparison " + s1.compareTo(s2)
 			   + " " + s1.compareTo(s3)
 			   + " " + s2.compareTo(s1));

 	System.out.println(" Integer Comparison " + e1.compareTo(e2)
 			   + " " + e1.compareTo(e3)
 	 		   + " " + e2.compareTo(e1));

If you notice the above example code, You would see that both String and Integer classes implement the Comparable Interface. Let us Compile/run the program, Here is the output:

  String Comparison -7 0 7
  Integer Comparison -1 0 1

comments powered by Disqus