Java Comparable vs Comparator
Sujit Kumar
Zenolocity LLC © 2013-2023
java.lang.Comparable Interface
• Allows an object of a class to be compared to
objects of the same class using a 1 or more
attributes of the same class.
• compareTo method is the ONLY member of
the Comparable interface. Allows objects of a
class to be sorted by natural ordering.
• Implementing Comparable allows:
• calling Collections.sort & Collections.binarySearch
• calling Arrays.sort and Arrays.binarySearch
• using objects as keys in a TreeMap
• using objects as elements in a TreeSet
compareTo method
• Return value is int, 0 => equal, -ve => less than and +ve
=> greater than
• anticommutation :
x.compareTo(y) = - (y.compareTo(x))
• exception symmetry :
x.compareTo(y) throws exactly the same exceptions
as y.compareTo(x)
• transitivity :
if x.compareTo(y) > 0 and y.compareTo(z) > 0,
then x.compareTo(z) > 0 (and same for less than)
if x.compareTo(y) == 0, then x.compareTo(z) has the same sign
as y.compareTo(z)
Example Implementation of
compareTo
public int compareTo(Employee emp) {
int result = this.id.compareTo(emp.getId());
if (result == 0) {
result = this.age > emp.age ? 1 : this.age < emp.age
? -1 : 0;
}
return result;
}
Performance Considerations
• One can greatly increase the performance
of compareTo by comparing first on attributes
which are most likely to differ.
• Preferable to always sort items in an RDBMS
first using ORDER BY instead of in memory in
java using Comparable.
java.util.Comparator Interface
• public int compare (Object o1, Object o2);
• Logical difference between Comparator and
Comparable is :
Comparator in Java compares any two objects,
while Comparable interface compares the
"this" reference with the object specified.
• Return value semantics is same as the
compareTo method of Comparable interface.
Guidelines on when to use
Comparable and Comparator
• For natural (intuitive) ordering use Comparable. Use
Comparator if you want to have an ordering different
from the natural order. If there is more than one
intuitive comparison possible, use a Comparator.
• Use comparable if class is in your control. Use
Comparator if class is NOT in your control and you cannot
make the class author implement Comparable.
• Comparable should be used when you compare instances
of same class (homogeneous). Comparator can be used
to compare instances of same or different classes
(homogeneous and heterogeneous).
Guidelines (continued…)
• Comparator has a distinct advantage of being
self descriptive. Examples:
• If you are writing Comparator to compare two
Employees based upon the salary then name
that comparator as SalaryComparator.
• If you are writing Comparator to compare two
Employees based upon the age then name
that comparator as AgeComparator.
Examples of Comparator
public static Comparator<Employee> EmployeeAgeComparator
= new Comparator<Employee>() {
public int compare(Employee emp1, Employee emp2) {
return emp1.age.compareTo(emp2);
}
};
public static Comparator<Employee> EmployeeNameComparator
= new Comparator<Employee>() {
public int compare(Employee emp1, Employee emp2) {
return emp1.name.compareTo(emp2)
}
};
How to Remember?
• Am I (this) Comparable to (compareTo) my
friend?
• Can I compare (Comparator) apples to
oranges?

Java Comparable and Comparator

  • 1.
    Java Comparable vsComparator Sujit Kumar Zenolocity LLC © 2013-2023
  • 2.
    java.lang.Comparable Interface • Allowsan object of a class to be compared to objects of the same class using a 1 or more attributes of the same class. • compareTo method is the ONLY member of the Comparable interface. Allows objects of a class to be sorted by natural ordering. • Implementing Comparable allows: • calling Collections.sort & Collections.binarySearch • calling Arrays.sort and Arrays.binarySearch • using objects as keys in a TreeMap • using objects as elements in a TreeSet
  • 3.
    compareTo method • Returnvalue is int, 0 => equal, -ve => less than and +ve => greater than • anticommutation : x.compareTo(y) = - (y.compareTo(x)) • exception symmetry : x.compareTo(y) throws exactly the same exceptions as y.compareTo(x) • transitivity : if x.compareTo(y) > 0 and y.compareTo(z) > 0, then x.compareTo(z) > 0 (and same for less than) if x.compareTo(y) == 0, then x.compareTo(z) has the same sign as y.compareTo(z)
  • 4.
    Example Implementation of compareTo publicint compareTo(Employee emp) { int result = this.id.compareTo(emp.getId()); if (result == 0) { result = this.age > emp.age ? 1 : this.age < emp.age ? -1 : 0; } return result; }
  • 5.
    Performance Considerations • Onecan greatly increase the performance of compareTo by comparing first on attributes which are most likely to differ. • Preferable to always sort items in an RDBMS first using ORDER BY instead of in memory in java using Comparable.
  • 6.
    java.util.Comparator Interface • publicint compare (Object o1, Object o2); • Logical difference between Comparator and Comparable is : Comparator in Java compares any two objects, while Comparable interface compares the "this" reference with the object specified. • Return value semantics is same as the compareTo method of Comparable interface.
  • 7.
    Guidelines on whento use Comparable and Comparator • For natural (intuitive) ordering use Comparable. Use Comparator if you want to have an ordering different from the natural order. If there is more than one intuitive comparison possible, use a Comparator. • Use comparable if class is in your control. Use Comparator if class is NOT in your control and you cannot make the class author implement Comparable. • Comparable should be used when you compare instances of same class (homogeneous). Comparator can be used to compare instances of same or different classes (homogeneous and heterogeneous).
  • 8.
    Guidelines (continued…) • Comparatorhas a distinct advantage of being self descriptive. Examples: • If you are writing Comparator to compare two Employees based upon the salary then name that comparator as SalaryComparator. • If you are writing Comparator to compare two Employees based upon the age then name that comparator as AgeComparator.
  • 9.
    Examples of Comparator publicstatic Comparator<Employee> EmployeeAgeComparator = new Comparator<Employee>() { public int compare(Employee emp1, Employee emp2) { return emp1.age.compareTo(emp2); } }; public static Comparator<Employee> EmployeeNameComparator = new Comparator<Employee>() { public int compare(Employee emp1, Employee emp2) { return emp1.name.compareTo(emp2) } };
  • 10.
    How to Remember? •Am I (this) Comparable to (compareTo) my friend? • Can I compare (Comparator) apples to oranges?