Tuesday, 12 June 2012

Difference between Comparable and Comparator interface and usage


Comparable interface provide compareTo method which can be used to do sorting in natural order but the Comparator interface provide the method compare which takes two arguments which can be used to sort based on more than one parameter.

For instance I have created a class UserDetails and comparing via Comparable interface and Comparator interface. Using comparator interface I am comparing the fname, in case the fname is equal then the sorting is done based on lName.

------------------------------------------------------------------------
public class UserDetails implements Comparable {
String fName;
String lName;

public UserDetails(String fName, String lName) {
super();
this.fName = fName;
this.lName = lName;
}

public String getfName() {
return fName;
}

public void setfName(String fName) {
this.fName = fName;
}

public String getlName() {
return lName;
}

public void setlName(String lName) {
this.lName = lName;
}

@Override
public int compareTo(Object o) {
// System.out.println("inside compareTo");
return getfName().compareTo(((UserDetails) o).getfName());
}

public String toString() {
return fName + " : " + lName;
}
}

------------------------------------------------------------------------

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class NameComparator implements Comparator {

// this comparator compares the first name and if its same it compares with
// the last name and gives the output
@Override
public int compare(Object o1, Object o2) {
UserDetails userDetails1 = (UserDetails) o1;
UserDetails userDetails2 = (UserDetails) o2;
if (userDetails1.getfName().equals(userDetails1.getfName())) {
return userDetails1.getlName().compareTo(userDetails1.getlName());
}else {
return userDetails1.getfName().compareTo(userDetails1.getfName());
}
}

public static void main(String args[]) {
List<UserDetails> userList = new ArrayList<UserDetails>();
userList.add(new UserDetails("Prajith", "k"));
userList.add(new UserDetails("Prajith", "a"));
userList.add(new UserDetails("Prajith", "A"));
userList.add(new UserDetails("Chandan", "urs"));
userList.add(new UserDetails("Gayatri", "c"));
userList.add(new UserDetails("Geeta", "P"));
userList.add(new UserDetails("Ajay", "kumar"));
userList.add(new UserDetails("Beena", "P"));
userList.add(new UserDetails("Akita", "D"));
/*
*Comparing using the comparator interface implementation 
* In assending order of the  fname and lname,
*/
Collections.sort(userList, new NameComparator());
System.out.println("Name Comparator output :  " + userList);

// In desending order of the  fname and lname
Collections.sort(userList, Collections.reverseOrder(new NameComparator()));
System.out.println("Name Comparator output in decending order  : "
+ userList);
/*
*Comparing using the Comparable interface implementation 
* In assending order of the fname 
*/
Collections.sort(userList);
System.out.println("Name Comparator output using Comparable interface :  " + userList);

// In desending order of the fname
Collections.sort(userList, Collections.reverseOrder());
System.out.println("Name Comparator output using Comparable interface in decending order  : "+ userList);
}

}
------------------------------------------------------------------------

Output of the above program is as below.
------------------------------------------------------------------------

Name Comparator output :  [Prajith : k, Prajith : a, Prajith : A, Chandan : urs, Gayatri : c, Geeta : P, Ajay : kumar, Beena : P, Akita : D]

Name Comparator output in decending order  : [Prajith : k, Prajith : a, Prajith : A, Chandan : urs, Gayatri : c, Geeta : P, Ajay : kumar, Beena : P, Akita : D]

Name Comparator output using Comparable interface :  [Ajay : kumar, Akita : D, Beena : P, Chandan : urs, Gayatri : c, Geeta : P, Prajith : k, Prajith : a, Prajith : A]

Name Comparator output using Comparable interface in decending order  : [Prajith : k, Prajith : a, Prajith : A, Geeta : P, Gayatri : c, Chandan : urs, Beena : P, Akita : D, Ajay : kumar]
------------------------------------------------------------------------

No comments:

Post a Comment