Thursday, November 17, 2016

Java Comparator interface with Example

In this blog post, I will show you how to use Comparator interface in Collections Framework. Earlier,i have written a java collections tutorial to know more about Collections framework visit it. Now we go straight forward to discuss about Comparator interface in java.

Comparator interface available in java.util package. By using Comparator object we can define our own customized sorting that means to sort the elements of an array into ascending order or descending order.

This interface contains two methods:

1) public int compare(Object obj1,Object obj2)

It returns -ve if obj1 has come before obj2

It returns zero if obj1 and obj2 are equal

It returns positive if  obj1 has to come after obj2

2) public boolean equals(Object obj)

Whenever we are implementing Comparator interface compulsory we should provide implementation for compare method. But implementing equals() method  is optional because it is already available from Object class through Inheritance 

Comparator can be written as:

interface Comparator<T>

Here T represents the type of elements that means objects compared by the Comparator.

For example,to compare Integer objects, we can write a class that implements the Comparator as:

class Desc implements Comparator<Integer>

Now, Let me explain with example where sorting order is descending order:

import java.util.*;
class Desc
{
public static void main(String args[])
{
TreeSet ts=new TreeSet(new MyComparator());
ts.add(23);
ts.add(45);
ts.add(3);
ts.add(9);
ts.add(18);
ts.add(99);
System.out.println(ts);
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
Integer i1=(Integer)o1;
Integer i2=(Integer)o2;
if(i1<i2)
return +1;
else if(i1>i2)
return -1;
else
return 0;
}
}
Result:

  



Now let us see the flow control in MyComparator:

It compare first two objects like below:
compare(23,45)   ------> 45,23
Then next two objects like below
compare(3,23)    ------->45,23,3
compare( 9,3)     --------> 45,23,9,3
compare(18,9)    --------> 45,23,18,9,3
compare(18,99)  --------> 99,45,23,18,9,3


Recommended To Read: Basic Java Interview Questions

Note:

If we are not passing Comparator object JVM will always call compareTo() method which is meant for default natural sorting order.



 Let us check another example how to use Comparator interface in java, in this example we are taking String Objects where the sorting order is reverse of Alphabetical order.

import java.util.*;
class ComparatorDemo
{
public static void main(String args[])

{ 
TreeSet ts=new TreeSet(new MyComparator());
ts.add("pavan");
ts.add("mahesh");
ts.add("Ravi");
ts.add("arjun");
ts.add("eeswar");
ts.add("Deva");
System.out.println(ts);
}
} 
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=(String)o1;
String s2=(String)o2;
return s2.compareTo(s1);
}
}

Output:


 




 

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...