• 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

Question about Comparable and ClassCastException

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My study notes contain the following:

If the objects you add to a Set do not implement Comparable you will get a ClassCastException when you add the second object. The JVM is trying to cast this object to a Comparable in order to compare it to the first one.



Assuming this note is correct, why does the object first have to be cast to Comparable?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas,
Set needs to know whether the object is already in the set. To figure this out, it wants to call the compare method. Since this method is defined on the Comparable interface, Set needs to cast the object first. This casting makes the compare method available.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your study notes are incorrect though. This limitation only holds for TreeSet, and only if you have not used the constructor that takes an Comparator. HashSet and LinkedHashSet use hashCode and equals instead of compareTo, and the Comparator will have precedence over using compareTo.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
Thomas,
Set needs to know whether the object is already in the set. To figure this out, it wants to call the compare method. Since this method is defined on the Comparable interface, Set needs to cast the object first. This casting makes the compare method available.



Jeanne,

Is it correct to say that the set needs to do this cast because at runtime it sees only an Object?
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,

Thank you, I have corrected that. When this is all done my book & notes are most likely going to some soldier overseas so it wouldn't do to include all the errors.
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic