Comparable and Comparator Interfaces in Java

Introduction

 
In Java are the two interfaces, Comparable and Comparator. As their name suggest, they are used for comparison in Java. One is used to compare its own objects with other objects and the other is used to compare various objects in Java.
 

Comparable in Java

 
Comparable is an interface in Java. In Java, Comparable compares its own objects with another object. It uses a compareTo( ) method to compare two objects in Java. In Java, it is a component of the collection framework. It is present in the java.lang package in Java.
 
Example 
  1. package demo;  
  2. import java.util.ArrayList;  
  3. import java.util.Collections;  
  4. import java.util.List;  
  5. public class Demo implements Comparable < Demo >  
  6. {  
  7.         String players;  
  8.         Demo(String p)  
  9.         {  
  10.             this.players = p;  
  11.         }  
  12.         public String getPlayers()  
  13.         {  
  14.             return players;  
  15.         }  
  16.         public int compareTo(Demo o)  
  17.         {  
  18.             return (this.players).compareTo(o.players);  
  19.         }  
  20.         public static void main(String[] args)  
  21.         {  
  22.             List < Demo > al = new ArrayList < Demo > ();  
  23.             System.out.println("LIST IN SORTED WAY :");  
  24.             al.add(new Demo("SACHIN"));  
  25.             al.add(new Demo("RAINA"));  
  26.             al.add(new Demo("DHONI"));  
  27.             al.add(new Demo("VIRAT"));  
  28.             al.add(new Demo("GANGULY"));  
  29.             al.add(new Demo("ZAHEER"));  
  30.             al.add(new Demo("IRFAN"));  
  31.             al.add(new Demo("ANIL"));  
  32.             Collections.sort(al);  
  33.             for (Demo d: al)  
  34.             {  
  35.                 System.out.println(d.getPlayers());  
  36.             }  
  37.         }  
  38. }  
Output
 
comparable in java 
 

Comparator in Java

 
Comparator is also an interface in Java. In Java, the comparator compares two objects. It uses a compare( ) method to compare the two objects in Java. In Java, it is also a component of the collection framework. It is also present in the java.lang package in Java.
 
Example
  1. package demo;  
  2. import java.util.ArrayList;  
  3. import java.util.Collections;  
  4. import java.util.Comparator;  
  5. import java.util.List;  
  6. public class Demo implements Comparator<Demo>  
  7. {  
  8.     String department;  
  9.     int experience;  
  10.     Demo()  
  11.     {  
  12.     }  
  13.     Demo(String d, int e)  
  14.     {  
  15.         this.department = d;  
  16.         this.experience = e;  
  17.     }  
  18.     public String getDepartment()  
  19.     {  
  20.         return department;  
  21.     }  
  22.     public int getExperience()  
  23.     {  
  24.         return experience;  
  25.     }  
  26.     public int compare(Demo d, Demo d1)  
  27.     {  
  28.         return d.experience - d1.experience;  
  29.     }  
  30.     public static void main(String[] args)  
  31.     {  
  32.         List<Demo> al = new ArrayList<Demo>();  
  33.         al.add(new Demo("C.S."3));  
  34.         al.add(new Demo("E.C."5));  
  35.         al.add(new Demo("I.T."6));  
  36.         al.add(new Demo("E.N."8));  
  37.         al.add(new Demo("CIVIL"12));  
  38.         al.add(new Demo("M.E."9));  
  39.         al.add(new Demo("E.E.E."4));  
  40.         al.add(new Demo("B.T."2));  
  41.         Collections.sort(al, new Demo());  
  42.         System.out.println("LIST OF EXPERIENCE IN INCREASING ORDER");  
  43.         System.out.println("\tDeptt.   Experience\n");  
  44.         for (Demo c1 : al)  
  45.         System.out.println("\t" + c1.getDepartment() + "\t  " + c1.getExperience());  
  46.     }  
  47. }  
Output
 
comparator in java 
 

Differences between Comparable and Comparator in Java

  • The Comparable interface must be implemented by the class that wants to sort its objects.
  • The Comparator interface is not necessarily implemented by the class that wants to sort its objects. It can be implemented by any other class in Java.
  • The Comparable interface uses a compareTo( ) method to compare the objects.
  • The Comparator interface uses a compare( ) method to compare the objects.
  • For comparable, we use java.lang.Comparable.
  • For comparator, we use java.lang.Comparator.

Summary

 
This article explained the Comparable and Comparator interfaces and also their differences in Java.   


Similar Articles