Hi ,
I get a class cast exception in the following program after one employee object is added to the TreeSet. Plz tell me y?
This is my employee class
public class Employee
{
String name;
String empid;
Employee(String name,String id)
{
this.name=name;
empid=id;
}
Employee(String name)
{
this.name=name;
}
public String toString()
{
return "Employee Name: " + this.name + " Employee id: " + empid;
}
}
and this is my class that throws classcastexception
import java.util.*;
class CreateSortedSet
{
public static void main(String args[])
{
TreeSet<Employee> s1=new TreeSet<Employee>();
Employee e1=new Employee("Akshatha");
Employee e2=new Employee("uu");
Employee e3=new Employee("cc");
Employee e4=new Employee("dkshatha");
Employee e5=new Employee("ekshatha");
System.out.println("RRRRRRRRRRRRR1");
s1.add(e1);
System.out.println("RRRRRRRRRRRRR2");
s1.add(e3);
System.out.println("RRRRRRRRRRRRR3");
s1.add(e5);
s1.add(e2);
System.out.println("RRRRRRRRRRRRR4");
s1.add(e4);
System.out.println("RRRRRRRRRRRRR5");
Iterator<Employee> it=s1.iterator();
System.out.println("RRRRRRRRRRRRR6");
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Thanks,
Akshatha