• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

comparable and compartor

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for clearing doubt
 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator






How these methods will be used in sorting a list and for a TreeSet???
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic