Last time we have discussed the Comparable interface and now we are going to see how the Comparator interface works.

Comparator interface:-

  • The Comparator interface provides greater flexibility with ordering.
  • For example, if you consider the Student class described previously, the sorting of Students was restricted to sorting on GPA’s.
  • It was not possible to sort the student based on first name or some other criteria.
  • So now we are going to see how sorting flexibility can be enhanced using the Comparator interface.
  • The Comparator interface is a java.util package member.
  • It is used to compare objects in the custom order instead of the natural order.
  • To sort 2 objects in custom order Comparator interface provides two methods those are,

1)    compare()

2)    equals()

  • For example, it can be used to sort objects in an order other that the natural order. It is also used to sort objects that do not implement the Comparable interface.
  • To write a custom Comparator, we need to provide an implementation for the compare method in the interface.
  • The functionality of compare() method is similar as the method of compareTo() method of Comparable interface.
  • We have to pass those 2 objects as a arguments which are to be compared.

public int compare(object obj1, Object obj2)

  • This method compares two arguments for order and returns a negative integer if the first argument is less than second, returns zero if both are equal, and returns positive integer if the first argument is greater that the second.

class Student

{

String firstname, lastname;

int stdId = 0;

double GPA = 0.0;

public Student(String fname, String lname, int std, double GPA)

{

if(fname == null || lname == null || std == 0 || GPA == 0.0)

{

Throw new IllegalArgumentException();

}

this.fname = fname;

this.lname = lname;

this.std = std;

this.GPA = GPA;

}

public String fname()

{

return fname;

}

public String lname()

{

return lname;

}

public int std()

{

return std;

}

public double GPA()

{

return GPA;

}

}

  • Several classes can be created to compare the students based on firstname, lastname, or GPA.
  • The class NameComp given below implements the Comparator interface to compare students based on firstname.

import java.util.*;

public class NameComp implements Comparator

{

public int compare(Object o1, Object o2)

{

Return (((Student)o1).fname.compareTo(((Student)o2.fname));

}

}

java I/O Fundamentals

  • The class GradeComp given below implements the Comparator interface to compare students based on their GPA’s.

import java.util.*;

public class GradeComp implements Comparator

{

public int compare(Object o1, Object o2)

{

if(((Student)o1).GPA == ((Student)o2).GPA)

{

return 0;

}

else if (((Student)o1).GPA == ((Student)o2).GPA)

{

return -1;

}

else

{

return 1;

}

}

}

  • The ComparatorTest class given below tests the NameComp comparator. In the gien code notice that the name comparator is passed as a parameter to TreeSet.

import java.util.*;

public class ComparatorTest

{

public static void main(String args[])

{

Comparator c = new NameComp();

TreeSet stdSet = new TreeSet();

stdSet.add(new Student(“M1”,”H1”,101,4.0));

stdSet.add(new Student(“M2”,”H2”,102,2.8));

stdSet.add(new Student(“M3”,”H3”,103,3.6));

Object[] studentArray = stdSet.toArray();

Student s;

for(Object obj : studentArray)

{

sS = (Student) obj;

System.out.println(“Name = %s %s Id = %d GPA = %.lf\n”,s.fname(),s.lname(),s.std(),s.GPA());

}

}

}

Output:

Name = M1 Id = 001 GPA = 3.6

Name = M2 Lynn Id = 002 GPA = 2.8

Name = M3 Id = 003 GPA = 2.3

For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.

Written by Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Comment