This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Difference between Comparable and Comparator interfaces

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me the difference between Comparable and Comparator interface and which to use when with examples.

Thanks
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparable interface is defined in the java.lang package and is implemented by a class if the objects of this class can be compared to each other. The comparation can be done using the compareTo() method of this interface. This method should return 0 if the objects are equal, 1 if the argument is smaller and -1 if the argument is bigger than the object on which we're calling this method.

Here's an example of a comparable class. We define the rule for comparation: the RichMan object is bigger if it's int field "money" is greater than one of another RichMan .



Comparator interface is defined in the java.util package and is used for sorting collections, e.g., with Collections.sort method. It defines a method compare(T o1, T o2) which is used for defining a custom sort order. For example, you want to sort a list of RichMan objects not by their money amount, but using their name order. Here's how this can be done:


So to sort a list of RichMan objects by their names, you can say:

[ December 18, 2007: Message edited by: Serge Petunin ]
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
And also, the custom sort order can be specified for a sorted collection like TreeMap with a comparator as a constructor argument;

TreeMap()
Constructs a new, empty map, sorted according to the keys' natural order.

TreeMap(Comparator c)
Constructs a new, empty map, sorted according to the given comparator
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic