• 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 interface

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
What rae the cases/scenario in which a class should implement Comparable? What is the use of Comparable interface?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class needs to implement the Comparable interface in order to make a comparison against another object of the same type.

For example, consider the BigInteger class. For the primitive int, we have the relational operators. But you can't use those with an object reference.

So BigInteger implements Comparable so that we can make a comparison with another BigInteger.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Class should implement Comparable interface if you are sorting collection of objects.

For example if you have list of FavoriteColor objects and you want to sort them according to the priority of you favorite colors then you can define your own favorite color sorting order by implementing Comparable Interface.

In java some classes like Stirng, BigDecimal, Integer, Date ... implements the Comparable interface.

Hope this helps.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • 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 the Comarable interface and the Comparator interface?

-- Kaydell
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JavaDoc :


This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.



So instance of class that implements Comparable expected can be compare properly (not violating the contract) to other instance of same class.

Methods & classes that required input/elements object that implement Comparable
  • Collections.sort(java.util.List)
  • Arrays.sort(Object[])
  • TreeSet
  • TreeMap

  •  
    Keith Lynn
    Ranch Hand
    Posts: 2412
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can only use the Comparable interface by modifying a class definition. There are certain situations where you cannot modify a class definition. In this case you can create a Comparator to compare instances of a class definition.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic