Roopak Neevan's Blog

Archive for November 2008

Comparable Interface : Problem while adding object to TreeSet

Posted by: Naveen Kapoor on: November 21, 2008

public class X{
  public static void main(String [] arg){
     Set s=new TreeSet();
     s.add(new Person(20));
     s.add(new Person(10));
     System.out.println(s);
 }
}
class Person{
    int i;
    Person(int i){
         i = this.i; 
    }
}
Can anyone tell me why this code fragment shows me the “ClassCastException”, Using Java 2 1.5 compiler.
In the above code
Set s=new TreeSet();
Set S which is a TreeSet accepts an object [...]



  • mudassir: thanks ............. .................... did a great job for guiding
  • Anand V: This is an wonderful effort !! It really helps lots of parents to understand the procedures and shed light over the minor passport myths. My hea
  • Deepak Dandriyal: Very Good MAN thanks for sharing,,,,,,,,,,god bless