Solved

what is Comparator and what is comparable interface?

Posted on 2011-03-20
5
513 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
5 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35178041
0
 
LVL 92

Accepted Solution

by:
objects earned 500 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

808 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