Results 1 to 8 of 8
Thread: Comparable and Comparator
- 04-21-2011, 04:59 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 10
- Rep Power
- 0
- 04-21-2011, 05:09 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
They are similar, googling will answer for the most part.
Comparator forces you to implement compareTo, which makes the object this(keyword), compare itself to the argument object.
With comparable, it compares two objects.
Others may have to give more details, perhaps I will learn something, I am having a hard time conveying my thoughts fully.Java Code:Comparable enforces int compareTo(T o) Compares this object with the specified object for order. comparator enforces int compare(T o1, T o2) Compares its two arguments for order.
http://lmgtfy.com/?q=what+is+the+dif...arator+in+java
I believe this explanation will illustrate my thoughts.
Lets say you have a class for peoples addresses, it has names, phone numbers, and addresses. If you would like to sort it by each of these things which would you do?
You could have the address class implement comparable, the problem is it only allows you to sort one way. Perhaps you could have an inner class that implements comparable? This wouldn't work because it uses this to compare the argument with, the this in an inner class would be the inner class, not the actual class.
If you use comparators you can have an inner class which takes two objects and lets you compare and sort based on different things.
In the address example you can have 3 inner classes which all implement comparators. Each of these comparators would sort based on a different instance variable(address, name, number)Last edited by sunde887; 04-21-2011 at 05:17 AM.
- 04-21-2011, 05:10 AM #3
From the Collections class:
If you call the first method all the Objects in the List must implement Comparable. If you call the other sort method you must write your own class that implements Comparator and pass an object of that class to the method. They basically do the same thing. It just depends upon your design.Java Code:sort(List<T> list) sort(List<T> list, Comparator<? super T> c)
-
Comparable is for when you want to sort a class by its "natural order", the one and main way it should be sorted. For it to work, the objects being sorted must implement the Comparable interface and have the proper compareTo method. The Comparator is for when the objects being sorted do not implement Comparable and so you must create a separate class that implements Comparator and usually has a single public method, compare.
- 04-21-2011, 05:13 AM #5
Also, imagine you have a Collection of people (Person class). If you use the Comparator way to sort, then you can have 2 or more Comparators: one to sort by last name, one to sort by first name, one to sort by address etc. If the Person class implements Comparable then you are stuck with sorting in only one way.
- 04-21-2011, 06:09 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Also, oftentimes you will not be able to rewrite (other people's) classes to make them implement Comparable and that's when writting a Comparator for them is the only way to sort them.
- 04-23-2011, 07:50 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Comparable :
It uses the compareTo() method.
int objectOne.compareTo(objectTwo).
It is necessary to modify the class whose instance is going to be sorted.
Only one sort sequence can be created.
It is frequently used by the API classes.
Comparator:
it uses the compare() method.
int compare(ObjOne, ObjTwo)
A separate class can be created in order to sort the instances.
Many sort sequences can be created.
It used by third-party classes to sort instances.
- 04-23-2011, 07:59 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
There's another difference: an object implementing the Comparable<T> interface can do what it wants because the interface implementation is part of the object. A Comparator<T> implementation can only compare those details from an object that are made visible to the outside world.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
sorting by Comparator
By Dayanand in forum New To JavaReplies: 4Last Post: 03-11-2011, 10:38 AM -
how to use comparator
By KidneyinaCooler in forum Advanced JavaReplies: 2Last Post: 07-18-2010, 10:25 AM -
Got hit by the Comparable block again, help pls.
By niu_niu in forum New To JavaReplies: 7Last Post: 06-24-2010, 09:49 AM -
Comparable Interface
By Yelrubk in forum New To JavaReplies: 3Last Post: 04-28-2010, 02:46 PM -
Using Comparable and Comparator interfaces
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks