?
Solved

what is Comparator and what is comparable interface?

Posted on 2011-03-20
5
Medium Priority
?
516 Views
Last Modified: 2012-05-11
Dear Experts,

Please tell me what is the use of Comparator interface & Comparable Interface, if both the interface are for the same purpose, please tell me when we will use comparator and when we will use Comparable, please explain in brief with examples.
0
Comment
Question by:haneef_nb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 35178061
Comparable is for defining the natural ordering for a class
Whereas Comparator allows you to define multiple alternative sort ordering

heres a Comparator example
0
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 35196366
From my end,

Comparator interface can be used to compare the object for that source code is not available with us.

Comparable interface can be used when we have source code available and we can able to implement compareTo(Object) method.

0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 35223469
Comparator is a specialization of the Strategy pattern. It identifies a class as one having the ability to compare two objects of a specified type, usually a different type than the Comparator such that the Comparator is not comparing its own instances but instances of some other class. I say it specializes Strategy because comparison is typically used in the context of sorting. A Comparator is the strategy provided to a given sorting algorithm for determining whether object A is less than, equal to or greater than an object B (both of the same type). A class implementing Comparator is usually very specialized in the sense that its entire existence is for the purpose of acting as the Comparator.

Comparable, on the other hand, is an interface implemented by a class declaring that it knows how to compare itself with another object, usually but not necessarily of the same type. When a class implements Comparable on its own type (e.g., class String implements Comparable<String>) then it defines a natural ordering for objects of that class. Objects of such a class can be sorted without further implementation by Arrays.sort and Collections.sort and used as keys in a SortedMap or elements in a SortedSet. AFAIK it is possible to implement Comparable genericized a a different type but that would probably be unwise as it breaks the implied contract of natural order for a class as defined in the Comparable javadoc.

Although a class could implement both Comparable and Comparator (because the methods declared by the interfaces have different names and signatures) you don't typically see that. Primarily that is again due to the specialized nature of classes created as Comparators v. the more general nature of classes that implement Comparable.

HTH. You'll find no end of additional resources by googling 'Comparable and Comparator'.

Regards,
Jim
0
 

Author Closing Comment

by:haneef_nb
ID: 35376543
Thanks....
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question