The Comparable interface of the Java library

  • The Java library contains many useful interfaces
    (An interface is like an abstract class !)

  • One of the Java's interfaces is the Comparable interface defined as follows:

    public interface Comparable<E> // Stored in java.lang package
    {
        public int compareTo(E o);
    } 

    which is the "super" class of all objects that can be compared

  • The documentation for the Comparable<T> interface is here:

  • Note:

    • The syntax <T> is called a generic type in Java

    • This is an advanced Java feature that we will study next (soon)

Java library methods that make use the Comparable interface

  • The Java library has many methods written for Comparable objects

  • The Arrays class contains many useful methods on Arrays

    Example 1: toString( arrayName )

      Arrays.toString( arrName )  converts an array to a string 
    

    Example 2: sort( arrayName )

      Arrays.sort( arrName )  sorts the items in the array
    


  • The (static) sort( ) method can sort the objects stored in an array, however:

      • The object class must implement the Comparable interface in order to for the objects to be sorted !

Example to use Arrays.sort( ) to sort an array of Integer

Example program that uses Arrays.sort( ) to sort an array of Integer:

    public static void main(String[] args)
    {  
         Integer[] arr = new Integer[4];

         arr[0] =  4;
         arr[1] =  2;
         arr[2] =  5;
         arr[3] =  1;

         for ( int i = 0; i < arr.length; i++ )
             System.out.print( arr[i] + " ");
	 System.out.println();
  
         Arrays.sort( arr );  // Sort array

         for ( int i = 0; i < arr.length; i++ )
             System.out.print( arr[i] + " ");
	 System.out.println();
    } 

DEMO: demo/05-interfaces/23-java-comparable/Demo.java

Note: we can use Arrays.sort( ) with an array of Integer because Integer implemented the Comparable interface

What happens when we use Arrays.sort( ) to sort an array of Circle object ???

Suppose we use Arrays.sort( ) to sort and array of Circle objects:

    public static void main(String[] args)
    {  
         Circle[] arr = new Circle[4];

         arr[0] = new Circle("red", 4);
         arr[1] = new Circle("green", 3);
         arr[2] = new Circle("blue", 5);
         arr[3] = new Circle("yellow", 2);

         for ( int i = 0; i < arr.length; i++ )
             System.out.print( arr[i] + " , ");
         System.out.println();

         Arrays.sort( arr );  // Sort Circle objects...

         for ( int i = 0; i < arr.length; i++ )
             System.out.print( arr[i] + " , ");
         System.out.println();

    }
  

DEMO: demo/05-interfaces/23-java-comparable/Demo2.java + Circle.java --- compile and run: crash !!!

Why it crashed:   the Circle class has not implemented the Comparable interface !!

Implementing the Comparable interface for the Circle class

In order to use the Arrays.sort( ) method with Circle objects, it must implement the Comparable interface:

public class Circle extends GeometricObject
{
    private double radius;

    // ... other method omitted for brevity

    // Arrays.sort( ) will only work with Comparable objects
    // The Circle class must implement the Comparable interface
    // in order to use Arrays.sort( )

   
   
       
                      
       
   
}

 

Implementing the Comparable interface for the Circle class

We must define the compareTo( ) method in the Circle class to implement the Comparable interface:

public class Circle extends GeometricObject implements Comparable<Circle> 
{
    private double radius;

    // ... other method omitted for brevity

    // Arrays.sort( ) will only work with Comparable objects
    // The Circle class must implement the Comparable interface
    // in order to use Arrays.sort( )

    public int compareTo( Circle other )
    {
        double diff = this.getArea() - other.getArea(); 

        return (int) Math.signum(diff);   // Convert to int: -1, 0 or +1
    }
} 

DEMO: demo/05-interfaces/24-java-comparable/Demo2.java + Circle.java

Postscript

  • Remember the ComparableThing interface that we studied in the previous slides:

    public interface ComparableThing
    {
        public int compareTo( ComparableThing x );
    }

  • I took the idea from the Java library:

         Interface Comparable<T>  

  • I also simplified it by removing the "generic type" <T> to make the examples understandable at your current level

    (We will study "generics" later (next) in the course....)