Comparable vs Comparator

  1. Comparator is an interface from java.util package.
  2. It includes two abstract methods – compare() and equals().
  3. A comparator object (a class that implements Comparator interface) can compare any two objects and these two objects are passed as parameters to compare().
  4. int compare(Object obj1, Object obj2).
  5. The Comparator object is used with Collections.sort(List, Comparator) andArrays.sort(Object[], Comparator) methods.
  6. Incase of Comparator, it is Collections.sort(studentList, esbm)
  1. Comparable is an interface from java.lang package.
  2. It includes only one abstract method compareTo()
  3. A comparable object (a class that implements Comparable interface) compares itself with another object.
  4. int compareTo(Object obj2)
  5. The Comparable object is used along with Collections.sort(List) andArrays.sort(Object[]) methods
  6. Incase of Comparable, it is Collections.sort(studentList)

Both methods return the same. Returns

  • A positive integer value if obj1 is greater than obj2.
  • 0(Zero) value if obj1 and obj2 are equal.
  • A negative integer value if obj1 is less than obj2.

 

Comparator Example

 import java.util.*;

// observe, Student does not implement Comparator 
class Student       
{
  private int marks;
  private String name;
 
  public Student(int marks, String name)
  {
    this.marks = marks;
    this.name = name;
  }
  public int getMarks()
  {
    return marks;
  }
  public String getName()
  {
    return name;
  }
}

// observe, StudentSortByMarks implements Comparator 
public class StudentSortByMarks implements Comparator<Student>
{                     
  public int compare(Student st1, Student st2) 
  {
    return st1.getMarks() - st2.getMarks();
  }
  public static void main(String args[])
  {
    List<Student> studentList = new ArrayList<Student>();
    studentList.add(new Student(50, "Rao"));
    studentList.add(new Student(40, "Seth"));
    studentList.add(new Student(60, "Kunal"));
    studentList.add(new Student(20, "Reddy"));
    studentList.add(new Student(30, "Nidhi"));
 
    StudentSortByMarks ssbm = new StudentSortByMarks();
    Collections.sort(studentList, ssbm);
    for(Student st : studentList)
    {
      System.out.println(st.getMarks() + " : " + st.getName());
    }
  }
}

Comparable Example

import java.util.*;
class Student implements Comparable<Student>
{
  private int marks;
  private String name;
 
  public Student(int marks, String name)
  {
    this.marks = marks;
    this.name = name;
  }
  public int getMarks()
  {
    return marks;
  }
  public String getName()
  {
    return name;
  }
  public int compareTo(Student st1)
  {
    return this.marks - st1.marks;
  }
}
 
public class StudentSortByMarks
{
  public static void main(String args[])
  {
    List<Student> studentList = new ArrayList<Student>();
    studentList.add(new Student(50, "Rao"));
    studentList.add(new Student(40, "Seth"));
    studentList.add(new Student(60, "Kunal"));
    studentList.add(new Student(20, "Reddy"));
    studentList.add(new Student(30, "Nidhi"));
 
    Collections.sort(studentList);
    for(Student st : studentList)
    {
      System.out.println(st.getMarks() + " : " + st.getName());
    }
  }
}
Advertisements

Tagged: , , ,

One thought on “Comparable vs Comparator

  1. Kamran Jelani May 10, 2013 at 10:26 am Reply

    Well Excellent description and presentation!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: