Hi, I Would Like To Know What Is The Main Difference Between Comparable & Comparator Interfaces. 1]I Would Like To Know When To Use Comparable Interface?. 2]I Would Like To Know When To Use Comparator Interface?. Can AnyBody Explains With Code Snippet?.
Krish, Comparable interface is implemented by all the wrapper classes(String, Float, Double....) and its called to represent class's natural ordering. Comparator's implementation is also the same except that u can explicitly create a class implementing comparator interface and change the way the SortedSet or SortedMap behaves. Look at the example here
The first part of this code is understandable. We just use a SortedSed to add elements sorted according to their natural ordering. So the output presented to u is quite understandable i guess. No problem in that.
But now look at the "UsingCmprtr " class which implements Comparator interface and have included the method compare() which always returns 0 meaning the objects are always equal(even though it violates the equal's contract).
Now in line 1, i have created a TreeSet object which uses Comparator interface to add objects into its collection. Remember the api for TreeSet
TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal.
So,the objects before getting inserted to the collection will be compared using compare method( as we have used the constructor "new TreeSet(cmptr); ") and if the objects are equal(means compare returns 0), the object will not be inserted into the collection. So, only one object remains in the TreeSet because all the objects return equal during comparison and as u know TreeSet does only let unique items in it. So u get the output as given. Hope its clear
Originally posted by Krish Pinnamaneni: Hi, I Would Like To Know What Is The Main Difference Between Comparable & Comparator Interfaces. 1]I Would Like To Know When To Use Comparable Interface?. 2]I Would Like To Know When To Use Comparator Interface?. Can AnyBody Explains With Code Snippet?.
In Advance, Thanks, Krish P.
Main difference (one that I can think of now):
Comparable - has only one method: [I]public int compareTo(Object o)
- the object stored in the collection is the one that implements this interface e.g.
Comparator - has 2 methods: == (1)abstract public int compare(Object o1, Object o2) == (2) public int equals(Object o)
- the class that implements this interface is the one that is passed to the constructor of the Tree
Any object can implement the Comparable interface, so that it can compare itself to another Object of its type. This is normally used in ordering things.
The Comparator is used to compare two objects of the same type. This is also normally used in ordering things.
Now the OO part of the difference is who is responsible for comparing objects. 1. The object itself (use the Comparable interface) 2. Some outside object (Use Comparator)
Now as an example, just yesterday I coded a Comparator, because I couldn't change the code of the object, and I wanted to sort an ArrayList. So I used the Comparator and passed it along with the Arraylist to the Collections.sort(List, Comparator) method.
Say for instance you wanted to change your sort order for Strings, well you can subclass String and implement Comparable, if you wanted to, but that leads to other problems. But you can create a Comparator that will take two Strings and compare based on your own criteria, then pass that into the Sorted Collection, or using the Collections.sort() method to sort based on your way of sorting.
I hope that helps clarify the difference and when you would use the two.