This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparable and compartor

 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between comparable and comparator.
when we use comparable and when we use comparator.

) both return int
in comparator
eg. int r=compare(obj1.obj2);
in comparable
eg. int r=obj1.compareTo(obj2);

ii)both returns
negative if obj1<obj2
zero if obj1==obj2
positive if obj1>obj2

am i right
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You speak of these interfaces as if they are methods. They are not. They are interfaces that encapsulate the same idea but in a subtly different way. A class is made to implement the Comparable interface if it has a natural way of ordering itself, for instance if you had a class to hold student information and if you would typically order this information based on the student's name, then you would make the class Comparable and have the compareTo method of the object compare last names followed by first names (if last names were equal). If on the other hand you had a class of Student information, but would typically order this in many different ways -- name, or grade, or student ID number, perhaps, then you would create separate Comparator classes (these are helper classes) that define each ordering that may be needed. Then when you wish to sort your listed Student information, you'd use an appropriate Comparator helper object in your call to the sort method.

Also note how the compareTo vs compare methods are different in that one has one parameter and the other has two.

Best of luck and hth.
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for clearing doubt
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didnot understand how the return values in compare and compareTo will effect the order of a collection.

Anyone please explain..
 
akash azal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


difference between return types

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

public static <T> void sort(List<T> list,
Comparator<? super T> c)
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






How these methods will be used in sorting a list and for a TreeSet???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic