Pages

Wednesday, September 1, 2010

Comparable and Comparator






In java the element in collections can be sorted by using TreeSet or TreeMap. To sort the data elements a class needs to implement Comparator or Comparable interface. Thats why all Wrapper classes like Integer,Double and String class implements Comparable interface.
Comparable Interface
A class implementing Comparable interface need to override compareTo(Object obj) method and put the logic for sorting. Comparable interface is in java.lang package.
This method returns an int value : -1,0,1
It will return -1 : If the object is lesser than the passed object
It will return  0 : If the object is equal to the passed object
It will return  1 : If the object is greater than the passed object

Ex: 

class Person{
public String name;
public String lastName;
public person(String name, String lastName){
this.name=name;
this.lastName;
}
public String getName(){
return name;
}
public String getLastName(){
return lastName;
}
public static void main(String args[]){
List myList  = new ArrayList();
myList.add(new Person("Vardhan", "India");
myList.add(new Person("Amit", "US");
myList.add(new Person("Mahesh", "UK");
for(Person p : myList){
System.out.println(" My Name is :::::::::::::::::: "+ p.getName());
}

Output:
My Name is :::::::::::::::::: Vardhan
My Name is :::::::::::::::::: Amit
My Name is :::::::::::::::::: Mahesh

But now i want that objects to be sorted on name basis should be retrieved in sort order

class Person impletements Comparable{
public String name;
public String lastName;
public person(String name, String lastName){
this.name=name;
this.lastName;
}
public String getName(){
return name;
}
public String getLastName(){
return lastName;
}
public int compareTo(Object obj){
Person p = (Person)obj;
return this.name.compareTo(p.getName());
}
public static void main(String args[]){
List myList  = new ArrayList();
myList.add(new Person("Vardhan", "India"));
myList.add(new Person("Amit", "US"));
myList.add(new Person("Mahesh", "UK"));
Collections.sort(myList);
for(Person p : myList){
System.out.println(" My Name is :::::::::::::::::: "+ p.getName());
}
}
Output:
My Name is :::::::::::::::::: Amit
My Name is :::::::::::::::::: Mahesh
My Name is :::::::::::::::::: Vardhan







Couple of things which needs to be taken in consideration:

1) Collections.sort() will sort only the collection having objects which implements either one of the comparing interface.
2) Collections.sort() will sort the same list.

Comparator Interface
Comparator interface is used when an extra logic is required to sort the objects. One need to override compare(Object obj1, Object obj2) method.  Comparator interface is in java.util package.
For example you want the list of Person object to be sorted on the basis of complete name i.e "name lastName" but also on the other hand doesnt want to change the Person class default sorting implementation or Person class is a jar so so no code modification in it can be done. First create a Custom Comparator.

public class MyCustomComaparator implements Comparator{
public int compare ( Object obj1, Object obj2){
Person p1 = (Person)obj1;
Person p2 = (Person) obj2;
String p1Name= p1.getName() + " " + p1.getLastName();
String p2Name= p2.getName() + " " + p2.getLastName();
return p1Name.compareTo(p2Name);
}

// Changes made in main method of Person class.
public static void main(String arg[]){
List myList = new ArrayList();
myList.add(new Person("Vardhan", "US"));
myList.add(new Person("Vardhan", "UK"));
myList.add(new Person("Vardhan", "India"));
Collections.sort(myList, new MyCustomComaparator());
for(Person p : myList){
System.out.println(" My Name is :::::::::::::::::: "+ p.getName() + "" + p.getLastName());
}
}
}

 
Output:
My Name is :::::::::::::::::: Vardhan India
My Name is :::::::::::::::::: Vardhan UK
My Name is :::::::::::::::::: Vardhan US

Couple of things which needs to be taken in consideration:

1) For Comparator interface you need to override method compare(obj a,obj b)

2) In collections.sort() the instance of Comparator need to be passed. In this example the list is sorted according to the custom Comparator created

No comments:

Post a Comment