Comparable interface is one of the key concepts of Java Collection Framework. The Comparator interface helps in ordering the object/elements. Unlike the Comparable Interface that provides the default/Natural ordering, Comparator interface gives the user control over the ordering. There are some Collection interfaces that do not have a natural ordering, and it is the Comparator Interfaces that provides ordering to such interfaces.
In the case of a SortedSet, the elements are sorted in Natural ordering. But, if you wish to change it or define your own sorting mechanism, Comparator would be the way to go. These Collections provide methods and Constructors which helps in choosing the Comparator. More details on those methods/constructors are available in Sorted Set Interface.
The ordering that is imposed by the Comparator should (Recommended) be consistent with equals() method. So, c.compare(e1,e2) == 0 should return the same boolean value as e1.equal(e2).
Comparator interface defines two methods. The first method, "int compareTo(Object o)". This method returns either "Zero" or "-ve number" or a "+ve number". Let us explain the behavior of this method using an example.
The statement "e1.compareTo(e2)" return 0 if e1 = e2; The statement "e1.compareTo(e2)" return -1 if e1 < e2; The statement "e1.compareTo(e2)" return +1 if e1 > e2.
The second method that is provided by the Comparator interface is "equals(Object o). This method returns True if both the object are equal.
Method | Description |
---|---|
int compare(T o1, T o2) | Compare two Arguments for the order |
bool equals(Object o1) | Compare the object "o1" with the object that invokes this method. |
Here is an example program, that demonstrates the usage of Comparator. A TreeSet has been used in the below example, TreeSet is an implementation class of SortedSet interface, more detailed information is available here; Sorted Set and Tree Set.
import java.util.Comparator; import java.util.TreeSet; class ComparatorClassDemo implements Comparator { @Override public int compare(Object arg0, Object arg1) { Integer x = (Integer) arg0; Integer y = (Integer) arg1; if (x < y) return 1; else if (x > y) return -1; else return 0; } } public class ComparatorDemo { public static void main(String[] args) { TreeSet tsetObj1 = new TreeSet(); TreeSet tsetObj2 = new TreeSet(new ComparatorClassDemo()); tsetObj1.add(4); tsetObj1.add(24); tsetObj1.add(13); tsetObj2.add(4); tsetObj2.add(24); tsetObj2.add(13); System.out.println(" TreeSet1: " + tsetObj1); System.out.println(" TreeSet2: " + tsetObj2); System.out.println(" Comparator Object: " + tsetObj2.comparator() + ", Default Object (Comparable): " + tsetObj1.comparator()); } }
All elements of a SortedSet are ordered using their natural ordering. So, all the elements of the tsetObj1 will be in ascending order which is the default/Natural order. Now, with the second set represented by tsetObj2, the order has been changed with the help of a Comparator. Notice the way, the second set has been created "TreeSet tsetObj2 = new TreeSet(new ComparatorClassDemo());", the Comparator that is passed as an argument implements the compare() method and the elements of the second set are compared using compare() method rather than the default compareTo() method which is part of the Comparable interface.
Let us Compile/run the program, Here is the output:
TreeSet1: [4, 13, 24] TreeSet2: [24, 13, 4] Comparator Object: ComparatorClassDemo@669aa3f3, Default Object (Comparable): null
Notice the logic that is present in the Overridden compare() method, it makes sure the Set is ordered in descending order. More details about the methods and Constructors, of a TreeSet class is available here TreeSet Methods and Constructors.