Solved

what is Comparator and what is comparable interface?

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

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
network + 7 73
Free Alternative to JIRA 4 55
Java passing arguments (type error) 2 35
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 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

20 Experts available now in Live!

Get 1:1 Help Now