• 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

testtree.Book cannot be cast to java.lang.Comparable

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting an error in this code that says "Exception in thread "main" java.lang.ClassCastException: testtree.Book cannot be cast to java.lang.Comparable".

I'm running this in netBeans. What do I need to do to correct the code?

Thank you.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

TreeSet sorts its elements. And there are two ways for you to help it with that. You can provide a Comparator when constructing the TreeSet. Or you can make the elements Comparable.

If you don't provide either, that is the exception that you will get from the TreeSet.

Henry
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you don't want them sorted, just use a HashSet instead of a TreeSet.
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry, Thanks for your help. Can you show me what the code would look like if I provided a Comparator when constructing the TreeSet? Thank you.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The important thing to ask when sorting objects is "does this class have a natural ordering". In other words, is there a sensible, default ordering that you'd want to use in the majority of case? If so, the best approach is to make the class implement Comparable. In this case, presumably you want to sort alphabetically by title? Then you can do this:
If you've done that, the TreeSet will just work as you want it.

If the class doesn't have a natural ordering, or if you don't want to use the natural ordering in this particular case, then that's when you need a Comparator. The code for creating the TreeSet with a Comparator is dead simple - there's a constructor that takes a Comparator as an argument: To create the comparator, just implement Comparator<Book>. The Java tutorials have a section giving more detail on all this at http://download.oracle.com/javase/tutorial/collections/interfaces/order.html.

 
Ranch Hand
Posts: 88
1
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! That helped me too.
Thanks for the link Matthew Brown!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic