Solved

what is Comparator and what is comparable interface?

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 65
Windows 10 and JDK 7 7u85 4 39
spring jars download 1 26
What's wrong with this code? 4 21
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

816 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

11 Experts available now in Live!

Get 1:1 Help Now