Comparable and Comparator in Java with examples

Firstly, This tutorial describes the detailed information about the difference between interfaces comparable and comparator in Java with examples.
And we will learn the usage of both comparable and comparator.
Firstly we learn about comparable.

Comparable

Comparable is actually enables the classes the ability to compare the instances.
The interface has a method compareTo() it takes the argument T and returns an integer.
The syntax is:
intobjX.compareTo(objY)

  • This method returns a negative value if objX is less than objY.
  • Returns Zero if objX is similar to objY.
  • Returns positive integer if objX is greater than objY.

This Interface is mainly used to sort the objects.
Comparable interface is present in java.lang.package.
To sort the elements of comparable we can use the method collections.sort(List,comparable)

#Java code.

Here is the example of a comparable interface that sorts the list of elements based on the age.

class Employee implements Comparable<Student>{  
int id;  
String name;  
int age;  
Employee(int id,String name,int age){  
this.id=id;  
this.name=name;  
this.age=age;  
}  
  
public int compareTo(Employee emp){  
if(age==emp.age)  
return 0;  
else if(age>emp.age)  
return 1;  
else  
return -1;  
}  
}  
import java.util.*;  
public class Sort1{  
public static void main(String args[]){  
ArrayList<Employee> al=new ArrayList<Employee>();  
al.add(new Employee(101,"Vineetha",23));  
al.add(new Employee(106,"swati",27));  
al.add(new Employee(105,"sindhu",21));  
  
Collections.sort(al);  
for(Employee emp:al){  
System.out.println(emp.id+" "+emp.name+" "+emp.age);  
}  
}  
}
105 sindhu 21
101 swati 23
106 vineetha 27

 

Secondly, we will see the comparator.

Comparator

This interface has a method compare().
The syntax is:

int compare(objX,objY).

This method also returns the same values as comparable.
In comparator, we are able to create many sorting sequences.
Comparable interface is present in java.util package.

To sort the elements of comparator we can use the method collections.sort(List, Comparator).

#Java code.

The following code sorts the element using Comparator interface.

import java.util.*; 
import java.lang.*; 
import java.io.*; 
class Student 
{ 
  int id; 
  String name, address;  
  public Student(int id, String name, String address) 
  { 
    this.id = id; 
    this.name = name; 
    this.address = address; 
  } 
  public String toString() 
  { 
    return this.id + " " + this.name + " " + this.address; 
  } 
} 

class Sortbyid implements Comparator<Student> 
{ 
  public int compare(Student a, Student b) 
  { 
    return a.id- b.id; 
  } 
} 

class Sortbyname implements Comparator<Student> 
{ 
  public int compare(Student a, Student b) 
  { 
    return a.name.compareTo(b.name); 
  } 
} 

class Main 
{ 
  public static void main (String[] args) 
  { 
    ArrayList<Student> ar = new ArrayList<Student>(); 
    ar.add(new Student(570, "abdhul", "london")); 
    ar.add(new Student(561, "arjun", "nyc")); 
    ar.add(new Student(588, "vinnie", "jaipur")); 

    System.out.println("Unsorted"); 
    for (int i=0; i<ar.size(); i++) 
      System.out.println(ar.get(i)); 

    Collections.sort(ar, new Sortbyid()); 

    System.out.println("\nSorted by id"); 
    for (int i=0; i<ar.size(); i++) 
      System.out.println(ar.get(i)); 

    Collections.sort(ar, new Sortbyname()); 

    System.out.println("\nSorted by name"); 
    for (int i=0; i<ar.size(); i++) 
      System.out.println(ar.get(i)); 
  } 
 }

The above code sorts the elements based on the name and id.

Unsorted
570 abdhul london
561 arjun nyc
562 vinnie jaipur

Sorted by id
561 arjun nyc
562 vinnie jaipur
570 abdhul london

Sorted by name
570 abdhul london
561 arjun nyc
562 vinnie jaipur

 

finally, these two are must not be used interchangeably, these two are two different interfaces and they have their own purpose.
also, read

Leave a Reply

Your email address will not be published. Required fields are marked *