• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Comparable and Comparator

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chap 7 states that:
Comparable has a single method : compareTo()
and
Comparator has only one method : Compare()

Pg 554 has a small code as follows:



My doubt is that is it okay to use compareTo method without implementing the comparable???
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly you mean by "Is it Okay". Is it legal?
Yes. You can either use comparable by not specifying a comparator while instantiating or you can chose to use a comparator without/with your objects implementing comparable.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling compareTo() on the object that is returned by one.getGenre().

The class of that object has a compareTo() method, otherwise this code wouldn't compile. It doesn't matter if that compareTo() method is there because the class implements Comparable or not.
 
Sow
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,

Based the two replies - can we say that compareTo() can be used with or without implementing the comparable. Correct me if i am wrong...

Thanks,
Sowmiya.
 
Author
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sowmiya girish:
Based the two replies - can we say that compareTo() can be used with or without implementing the comparable. Correct me if i am wrong...


Sure you can, but keep in mind that without saying that your class implements Comparable, you will not be able to pass a class instance to a method expecting a parameter that "is-a" Comparable. For example, the version of Collections.max that takes a Comparable:

Hope that helps!
 
A teeny tiny vulgar attempt to get you to buy our stuff
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic