posted 16 years ago
add() API of TreeSet class
Throws:
ClassCastException - if the specified object cannot be compared with the elements currently in the set.
Your Person class does not implement Comparable interface and implement the comaareTo() of Comparable interface. Hence add() fails to add Person object and throws ClassCastException.
Note that ClassCastException is thrown only when you add more than 2 elements to the set. Its when you add second element, the second element will be compared to the first element, and since the class Person does not implement Comparable interface and hence ClassCastException is thrown.
Thanks
Deepak