File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion 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