Set s = new HashSet();
boolean b[] = new boolean[5];
b[0] = s.add("a");
b[1] = s.add(new Integer(5));
b[2]= s.add("a");
b[3] = s.add(new Object());
b[4] = s.add("4");
for(int i=0;i<b.length;i++){
System.out.println(b[i]);
}
This is giving me this output as expected : -
true
true
false
true
true
But when I am using Treeset
Set s = new TreeSet();
It is giving me this exception.
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at java.lang.Integer.compareTo(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at com.sunil.questions.TreeSetExample.main(TreeSetExample.java:15)
I want to know why it is showing me this behavior.