Solved

what is Comparator and what is comparable interface?

Posted on 2011-03-20
5
509 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now