[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

what is the difference between comparable and comparator interface

Posted on 2011-10-06
17
Medium Priority
?
476 Views
Last Modified: 2012-06-21
what is the difference between comparable and comparator interface
0
Comment
  • 11
  • 3
15 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36928865
Comparable need to define method:

int       compareTo(T o)

Comparator defines methods
int       compare(T o1, T o2)

and

 boolean       equals(Object obj)
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 36928870
what is the difference
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36928871
In order to sort Collection<E> its elemenst need to implemnet Comparable

public static <T extends Comparable<? super T>> void sort(List<T> list)

Bt you can aslo sort your own custom objects if you use
another method whaich takes type implementing Comaprator as arguments:

public static <T> void sort(List<T> list,
                            Comparator<? super T> c)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 47

Expert Comment

by:for_yan
ID: 36928873
Loook above they implemnent methods with different signatures
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36928875
Comparable takes one parameter and Comaparator takes two parameters
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36928876
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36928877

14 down vote accepted
      

The text below comes from http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html

Comparable

A comparable object is capable of comparing itself with another object. The class itself must implements the java.lang.Comparable interface in order to be able to compare its instances.

Comparator

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances. This comparator class must implement the java.util.Comparator interface.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36928880

This is also nice explanation:

Comparable lets a class implement its own comparison:

    it's in the same class (it is often an advantage)
    there can be only one implementation (so you can't use that if you want two different cases)

By comparison, Comparator is an external comparison:

    it is typically in a unique instance (either in the same class or in another place)
    you name each implementation with the way you want to sort things
    you can provide comparators for classes that you do not control
    the implementation is usable even if the first object is null
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 2000 total points
ID: 36928886
When you define your custom class and want to sort it you can make this class Comparable i.e. implement Comparable interface

Alternatively you may not touch the class, but rather define external class which implements Comparator and use this
external object as parameter of sorting methiods
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 37010218
thankyou for the analysis
0
 
LVL 9

Author Comment

by:Manikandan Thiagarajan
ID: 37010416
I've requested that this question be closed as follows:

Accepted answer: 0 points for stmani2005's comment http:/Q_27384931.html#37010218

for the following reason:

helpful
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37010247
so why did you request to close this without allcatiing points?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37010417
I believe this question was answered. Perhaps author just pressed the wrong button ?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37013052

I'd suggest that the solution should be split between posting http:#36928865 which explains
formal difference beween Comparable and Comparator in terms of method signatures
and http:#36928886 which shows the difference between them in terms of most popular
use cases.
0
 

Expert Comment

by:South Mod
ID: 37074927
All,
 
Following an 'Objection' by for_yan (at http://www.experts-exchange.com/Q_27409984.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
SouthMod
Community Support Moderator
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month18 days, 20 hours left to enroll

834 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