Results 1 to 2 of 2
- 07-29-2012, 09:30 AM #1
Member
- Join Date
- Jul 2012
- Posts
- 1
- Rep Power
- 0
Comparable vs. Comparator interface?
Hi,
I'm still having trouble understanding the difference with these two interface types.
When do we use comparable, and when do we use comparator? What can comparator do which comparable can't?
I understand that compareTo() compares a implicit parameter with a explicit parameter, where compare() compares two explicit parameters, but what does this difference mean?
- 07-30-2012, 11:41 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
Re: Comparable vs. Comparator interface?
Comparable is an interface generally implemented by a class that is Comparable.
In other words, a class which you want to assign a "natural order" to.
You use a Comparator to provide ordering for something that does not have a natural order, or whose order for a particular List (for example) is not the same as its natural order.Please do not ask for code as refusal often offends.
Similar Threads
-
Comparable interface
By diamonddragon in forum New To JavaReplies: 17Last Post: 02-01-2012, 12:07 AM -
How to use Comparator/Comparable to sort Hash Map of Objects by value within object
By dragstang86 in forum New To JavaReplies: 0Last Post: 10-04-2011, 10:45 PM -
Comparable and Comparator
By jeanjiang in forum New To JavaReplies: 7Last Post: 04-23-2011, 07:59 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