Comparator Interface in Java

Introduction 

 
This article teaches one of the Java concepts, the Comparator interface in Java, with simple examples, for a better understanding.
  

Comparator Interface

 
The Comparator Interface comes under the folder of a collection in Java, used to order the objects of the user-defined class. This interface resides in the java.util package and it contains two methods, compare(object obj1, object obj2) and equals(object elements).
  
In the comparator interface the sorting logic is written in a different class, so that different sorting logic can be used on the basis of multiple attributes, of the class.
Using the comparator interface, the class itself does not need to implement any interface, rather another class implements the Comparator interface, to sort the collection or array of objects using the compare() method.
  
Comparator's compare(obj1, obj2) compares ”obj1” object with “obj2” and returns an integer value. Returns 1 if (obj1>obj2), -1 if (obj1<obj2) and 0 if (obj1==obj2).
 
It provides multiple sorting sequences.  In other words, you can sort the elements based on any data member. For example name, roll number, age, or anything else.
 

Method of collection class for sorting list of elements

 

Public void sort(List list, Comparator c):

 Sorts the elements of the list, given by the comparator.
 

Example

 
Here we made five Java files and all five are for various purposes.
  • Student.java 
    This file includes the name, Id and roll no. and a parameterized constructor.
     
  • Id.java
    This file is developed for comparison of the student, on the basis of their Id.
     
  • Name.java
    This file is also used to compare the student, on the basis of name.  In this case we are using the compareTo() method of the String class that internally provides the comparison.
     
  • RollNo.java
    This file is also used to compare the student on the basis of roll no.
     
  • Test.java
    This file is made to test the working of the comparator interface that sorts the objects values, on the basis of age and name of the student.
Now, let's have a look at the example.
 

Student.java

  1. public class Student {  
  2.  private int stId;  
  3.  private String stName;  
  4.  private int stRollNo;  
  5.  Student(int stId, String stName, int stRollNo) {  
  6.   this.stId = stId;  
  7.   this.stName = stName;  
  8.   this.stRollNo = stRollNo;  
  9.  }  
  10.  public int getStId() {  
  11.   return stId;  
  12.  }  
  13.  public void setStId(int stId) {  
  14.   this.stId = stId;  
  15.  }  
  16.  public String getStName() {  
  17.   return stName;  
  18.  }  
  19.  public void setStName(String stName) {  
  20.   this.stName = stName;  
  21.  }  
  22.  public int getStRollNo() {  
  23.   return stRollNo;  
  24.  }  
  25.  public void setStRollNo(int stRollNo) {  
  26.   this.stRollNo = stRollNo;  
  27.  }  
  28. }  

 

Id.java

  1. import java.util.Comparator;  
  2. public class Id implements Comparator < Student > {  
  3.  public int compare(Student st1, Student st2) {  
  4.   return (st1.getStId() < st2.getStId()) ? -1 : (st1.getStId() > st2.getStId()) ? 1 : 0;  
  5.  }  
  6. }  

 

Name.java

  1. import java.util.Comparator;  
  2. public class Name implements Comparator < Student > {  
  3.  public int compare(Student st1, Student st2) {  
  4.   return (st1.getStName().compareTo(st2.getStName()));  
  5.  }  
  6. }  

 

RollNo.java

  1. import java.util.Comparator;  
  2. public class RollNo implements Comparator < Student > {  
  3.  public int compare(Student st1, Student st2) {  
  4.   return (st1.getStRollNo() < st2.getStRollNo()) ? -1 : (st1.getStRollNo() > st2.getStRollNo()) ? 1 : 0;  
  5.  }  
  6. }  

 

Test.java

  1. import java.util.ArrayList;  
  2. import java.util.Collections;  
  3. public class Test {  
  4.  public static void main(String args[]) {  
  5.   Student employee1 = new Student(13"Vikas"1004);  
  6.   Student employee2 = new Student(11"Ashish"1002);  
  7.   Student employee3 = new Student(14"Prashant"1001);  
  8.   Student employee4 = new Student(12"Kailash"1003);  
  9.   ArrayList < Student > empList = new ArrayList < Student > ();  
  10.   empList.add(employee1);  
  11.   empList.add(employee2);  
  12.   empList.add(employee3);  
  13.   empList.add(employee4);  
  14.   System.out.println("EmpList elemnets before sorting :");  
  15.   for (int i = 0; i < empList.size(); i++) {  
  16.    System.out.println("EmpId: " + empList.get(i).getStId() +  
  17.     " | EmpName: " + empList.get(i).getStName() + " | EmpRollNo: " + empList.get(i).getStRollNo());  
  18.   }  
  19.   Collections.sort(empList, new Id());  
  20.   System.out.println("EmpList elemnets after sorting by id :");  
  21.   for (int i = 0; i < empList.size(); i++) {  
  22.    System.out.println("EmpId: " + empList.get(i).getStId() +  
  23.     " | EmpName: " + empList.get(i).getStName() + " | EmpRollNo: " + empList.get(i).getStRollNo());  
  24.   }  
  25.   Collections.sort(empList, new Name());  
  26.   System.out.println("EmpList elemnets after sorting by name :");  
  27.   for (int i = 0; i < empList.size(); i++) {  
  28.    System.out.println("EmpId: " + empList.get(i).getStId() +  
  29.     " | EmpName: " + empList.get(i).getStName() + " | EmpRollNo: " + empList.get(i).getStRollNo());  
  30.   }  
  31.   Collections.sort(empList, new RollNo());  
  32.   System.out.println("EmpList elemnets after sorting by RollNO :");  
  33.   for (int i = 0; i < empList.size(); i++) {  
  34.    System.out.println("EmpId: " + empList.get(i).getStId() +  
  35.     " | EmpName: " + empList.get(i).getStName() + " | EmpRollNo: " + empList.get(i).getStRollNo());  
  36.   }  
  37.  }  
  38. }  

Output

 


Similar Articles