• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks! That helped me too.
Thanks for the link Matthew Brown!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!