The code:
public class FooFoo {
public static void main(String[] args) {
Set set = new HashSet<>();
set.add("hola");
set.add(2.3);
set.add(4.2F);
set.add(1);
TreeSet set2 = new TreeSet<>(set);
}
}
The error:
Exception in thread "main" java.lang.ClassCastException: java.lang.Float can not be cast to java.lang.Integer at java.lang.Integer.compareTo (Integer.java:52) at java.util.TreeMap.put (TreeMap.java:568) at java.util.TreeSet.add (TreeSet.java:255) at java.util.AbstractCollection.addAll (AbstractCollection.java:344) at java.util.TreeSet.addAll (TreeSet.java:312) at java.util.TreeSet. (TreeSet.java:160) at foo.FooFoo.main (FooFoo.java:19)
Why it fails, I do not understand.
Thanks in advance
Best regards