Home > Back-end >  Why do I get ClassCastException?
Why do I get ClassCastException?

Time:10-02

I am trying this piece of code. Why do I get ClassCastException when I have initialized my set to contain elements of type Object which is directly or indirectly parent of all classes in Java?

 Set s = new TreeSet<Object>();
    
    s.add(10);
    s.add("ABC");
    System.out.println(s);

CodePudding user response:

According to the Treeset javadoc, if you don't supply a Comparator when creating the TreeSet, it will use the element's natural ordering. That means that to determine the ordering of a pair of values, it will cast one value to Comparable<?> and then call compareTo to compare it to the other one.

In your example, the cast to Comparable<?> actually works, because both classes implement Comparable, albeit with different type parameters.

The problem is that this.compareTo(v) is defined to throw a ClassCastException when this and v are not comparable. The javadoc says:

"Throws: ClassCastException - if the specified object's type prevents it from being compared to this object."

When a is Double(10) and b is "ABC" and you call a.compareTo(b), the Double.compareTo method will attempt to cast b to Double. That cast will throw a ClassCastException.


In short, if you want to use a TreeSet to hold a mixture of different types, you must define and supply a Comparator that can order all of the types / elements you are going to add to the set.

CodePudding user response:

TreeSet have to compare objects somehow. If you don't provide Comparator explicitly it will try to cast objects to Comparable<T>.

String is implementing Comparable<String> so when TreeSet calls "ABC".compareTo(10) java tries to cast 10 to String and fails.

You should either use TreeSet with objects that are comparable to each other, or provide explicit comparator.

CodePudding user response:

You get ClassCastException because of not using type-safety when casting object (s).

Here are some options for you :

TreeSet s = new TreeSet();
Set syncSet = Collections.synchronziedSet(s);

(TreeSet is one of the most important implementations of the SortedSet interface in Java that uses a Tree for storage)

TreeSet in Java

  • Related