Gurgaon Jobs, Companies and Consultants

Those who are looking for job in Gurgaon can visit this site : Gurgaon Jobs. Here you will find email ids/contact no of "Companies in Gurgaon" and also of "Placement Consultants in Gurgaon"

Wednesday, April 08, 2009

Java Comparable vs Comparator

Both are used for sorting of java objects. but they are different from usage point of view.

Example Class : Employee has fields id, name, age.

java.util.Collections provides two methods which everybody uses for sorting in Java :

public static > void sort(List list)

Contract of this method is passed list elements MUST implement Comparable interface

public static void sort(List list, Comparator c)

Contract of this method is you have to pass your own defined Comparator

Now if you do not use neither Comparable nor Comparator what will happen :
In that at compile time it will give error when you will use Collections.sort();

Comparable :

Your Employee class must implement this interface which will force to write

public int compareTo(Employee employee1)

This method will return

+ve value if current object greater than employee1
-ve value if current object less than employee1
zero if current object equal to employee1

So if you want to sort by AGE of employee then compare that to return value.

Comparator :

You have to define new class like AgeComprator which will implement Comparator interface
that will force to write

public int compare(Employee emp1, Employee emp2)

This method will return
+ve value if emp1 greater than emp2
-ve value if emp1 less than emp2
zero if emp1 equal to emp2

So if you want to sort by AGE of employee then compare that field on emp1 and emp2
and accordingly return appropriate value.

Difference between two :

As you see in case of Comparable you can compare only by one of the field of Employee
either by Id, Name or Age. But using Comparator you can define three different comparators
and use any of them as per your need by passing in Collections.sort method. That's the basic difference between two.

General practice is to use Comparable for sorting in Natural order which can be based on any one of the field of Employee i.e natural sorting Otherwise if you need sorting by any of field of Employee then use Comparator.

In you program you can use BOTH use Comparable for natural ordering and use Comparator if you need to do sorting by any field other than implemented in Comparable for natural order.


Example of Comparator very good approach :

Instead of defining many classes IdComprator, NameComprator, AgeComprator,
use concept of enum so that it conveys to user what are options availaible for sorting.
See this :

public class EmpComparator implements Comparator {

________public enum SortOption {
__________SORT_BY_ID((short)1),
__________SORT_BY_NAME((short)2);

__________private short id;
__________private SortOption(short id) {
____________this.id = id;
__________}

__________public short getId() {
____________return id;
__________}
________}

________private SortOption selection;

________public
EmpComparator (SortOption selection) {
___________this.selection = selection;
________}

_______public int compare(
Employee emp1, Employee emp2){
________if(this.selection.compareTo(SortOption.SORT_BY_NAME) ==0) {
__________return
emp1.getName().compareTo(emp2.getName());
________} else {
__________return new Long(
emp1.getId()).compareTo(new Long(emp2.getId()));
________}
_______}
}

1 comment:

Vikash Sharma Niveriya said...

nice post.. one of the most clear and to the point post......