Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCast Exception while using TreeSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCast Exception while using TreeSet" Watch "ClassCast Exception while using TreeSet" New topic

ClassCast Exception while using TreeSet

Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643

Above code throws ClassCastException at line 6.
i know that for TreeSet its elements should be mutally comparable.
if i replaces Comparator with Comparable then class
compiles and runs fine but elements doesnot sorting.

I am not able to understand why
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Why does TreeSet.add throw ClassCastException?
I agree. Here's the link:
subject: ClassCast Exception while using TreeSet
jQuery in Action, 3rd edition