This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
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
 
Similar Threads
Sorted TreeSet from HashSet
making a mostly anonymous Comparator be Serializable
comparable and comparator interface
TreeXxx VS PriorityQueue
Doubt in Comparable interface