Popular Posts

Sunday, July 3, 2011

Comparable vs Comparator

Comparable :
A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator : 
A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.


How to use these?
There are two interfaces in Java to support these concepts, and each of these has one method to be implemented by user.
Those are:
java.lang.Comparable: int compareTo(Object o1)
This method compares this object with o1 object. Returned int value has the following meanings.
positive – this object is greater than o1
zero – this object equals to o1
negative – this object is less than o1

java.util.Comparator: int compare(Object o1, Objecto2)
This method compares o1 and o2 objects. Returned int value has the following meanings.
positive – o1 is greater than o2
zero – o1 equals to o2
negative – o1 is less than o1

java.util.Collections.sort(List) and java.util.Arrays.sort(Object[]) methods can be used to sort using natural ordering of objects.
java.util.Collections.sort(List, Comparator) and java.util.Arrays.sort(Object[], Comparator) methods can be used if a Comparator is available for comparison.
Refer : http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html

No comments:

Post a Comment