aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Diff. Between Comparable & Comparator Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Diff. Between Comparable & Comparator Interface" Watch "Diff. Between Comparable & Comparator Interface" New topic
Author

Diff. Between Comparable & Comparator Interface

Krish Pinnamaneni
Greenhorn

Joined: Mar 16, 2005
Posts: 22
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.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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




Output:

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
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

e.g.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Krish Pinnamaneni
Greenhorn

Joined: Mar 16, 2005
Posts: 22
Hi Animesh ,Alton Hernandez,Mark Spritzler

Thanks alot for spending your most valuable time for giving answer.

Thanks,
Krish Pinnamaneni.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Diff. Between Comparable & Comparator Interface
 
Similar Threads
Difference in between comparable and comparator interface
Mock Exam Question
difference between Comparator and Comparable interface
Difference between Comparable and Comparator interfaces
Collections and comparisons