Understanding Java Comparator Interface

Java Comparator Interface Definition

The Java Comparator interface, java.util.Comparator, allows you to compare two objects in order to sort them using Java's sorting functionality. For example, if you want to sort a Java List, you can pass it to a Java Comparator. During sorting, the Comparator compares the List objects. As opposed to Java Comparable, Java Comparator has a different interface.

A Comparator maintains a list of objects getting compared, while a Comparable maintains a list of getting compared objects. Package java.util contains the Java Comparator interface. Java Comparator has the following syntax:

public interface Comparator<T> {
    public int compare(T o1, T o2);
}


In comparison(), two objects are passed to the Comparator implementation to compare. When comparing two objects, compare() returns an int that indicates which was larger. This return value has the following semantics:

  • Objects with negative values are smaller than objects with positive values.
  • 0 indicates that both objects are equal.
  • If the value is positive, then the first object is larger than the second.

Java 8 Comparator Example

The following example shows how the List elements can get sorted based on the Students's age and name.

class Students {   
  int roll_no;   
  String f_name;   
  int age;   
    Students(int roll_no,String f_name,int age){   
    this.roll_no=roll_no;   
    this.f_name=f_name;   
    this.age=age;   
    } 
 
    public int getRoll_no() { 
        return roll_no; 
    } 
 
    public void setRoll_no(int roll_no) { 
        this.roll_no = roll_no; 
    } 
 
    public String getF_name() { 
        return f_name; 
    } 
 
    public void setF_name(String f_name) { 
        this.f_name = f_name; 
    } 
 
    public int getAge() { 
        return age; 
    } 
 
    public void setAge(int age) { 
        this.age = age; 
    } 
 
    }   


//File: TestSort1.java

  import java.util.*;   
  public class TestSort1{   
  public static void main(String args[]){   
  ArrayList<Students> al=new ArrayList<Students>();   
  al.add(new Students(301,"Raj",26));   
  al.add(new Students(302,"Mayuri",23));   
  al.add(new Students(303,"Priti",23));

   
//Name-based sorting of elements


  Comparator<Students> cmp=Comparator.comparing(Students::getF_name); 
  Collections.sort(al,cmp); 
  System.out.println("Sorting by Name"); 
  for(Students st: al){ 
    System.out.println(st.roll_no+" "+st.f_name+" "+st.age); 
    } 

   //Sorting elements based on age 
    Comparator<Students> cmp_2=Comparator.comparing(Students::getAge); 
Collections.sort(al,cmp_2); 
  System.out.println("Sorting by Age"); 
  for(Students st: al){ 
    System.out.println(st.roll_no+" "+st.f_name+" "+st.age); 
    }   
  }   
  }    


Output:


Sorting by Name
302 Mayuri 26
303 Priti 23
301 Raj 22

Sorting by Age
301 Raj 22
303 Priti 23
302 Mayuri 26


write your code here: Coding Playground

Conclusion

Now you have gone through a simple Comparator class example in Java and you should have a fair idea of how to use comparator example and the syntax to implement it. You can also try to solve new Comparator examples at your finger tips.