aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ClassCast Exception while using TreeSet" Watch "ClassCast Exception while using TreeSet" New topic
Author

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
thanks.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Why does TreeSet.add throw ClassCastException?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ClassCast Exception while using TreeSet