List to TreeSet conversion produces: "java.lang.ClassCastException: MyClass cannot be cast to java.lang.Comparable"
Asked Answered
A

3

11
List<MyClass> myclassList = (List<MyClass>) rs.get();

TreeSet<MyClass> myclassSet = new TreeSet<MyClass>(myclassList);

I don't understand why this code generates this:

java.lang.ClassCastException: MyClass cannot be cast to java.lang.Comparable

MyClass does not implement Comparable. I just want to use a Set to filter the unique elements of the List since my List contains unncessary duplicates.

Adrianaadriane answered 24/3, 2010 at 1:32 Comment(0)
C
18

Does MyClass implements Comparable<MyClass> or anything like that?

If not, then that's why.

For TreeSet, you either have to make the elements Comparable, or provide a Comparator. Otherwise TreeSet can't function since it wouldn't know how to order the elements.

Remember, TreeMap implements SortedSet, so it has to know how to order the elements one way or another.

You should familiarize yourself with how implementing Comparable defines natural ordering for objects of a given type.

The interface defines one method, compareTo, that must return a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than the other object respectively.

The contract requires that:

  • sgn(x.compareTo(y)) == -sgn(y.compareTo(x))
  • it's transitive: x.compareTo(y)>0 && y.compareTo(z)>0 implies x.compareTo(z)>0
  • x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)) for all z

Additionally, it recommends that:

  • (x.compareTo(y)==0) == (x.equals(y)), i.e. "consistent with equals

This may seem like much to digest at first, but really it's quite natural with how one defines total ordering.


If your objects can not be ordered one way or another, then a TreeSet wouldn't make sense. You may want to use a HashSet instead, which have its own contracts. You are likely to be required to @Override hashCode() and equals(Object) as appropriate for your type (see: Overriding equals and hashCode in Java)

Clardy answered 24/3, 2010 at 1:36 Comment(2)
MyClass does not implement Comparable. I just want to use a Set to filter the unique elements of the List since my List contains unncessary duplicates.Adrianaadriane
Use a HashSet then. It doesn't try to sort the elements.Krueger
K
2

If you don't pass an explicit Comparator to a TreeSet, it will try to compare the objects (by assuming they are Comparable). And if they aren't Comparable, it cannot compare them, so this exception is thrown!
TreeSets are sorted sets and require either objects to be Comparable or a Comparator to be passed in to determine how to sort the objects in the Set.

Krueger answered 24/3, 2010 at 1:36 Comment(0)
E
1

If you just want the set to remove duplicates, used a HashSet, although that will shuffle the order of the objects returned by the Iterator in ways that appear random.
But if you want to preserve the order somewhat, use LinkedHashSet, that will at least preserve the insertion order of the list.

TreeSet is only appropriate if you need the Set sorted, either by the Object's implementation of Comparable or by a custom Comparator passed to the TreeSet's constructor.

Elfriedeelfstan answered 24/3, 2010 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.