[英]List to TreeSet conversion produces: “java.lang.ClassCastException: MyClass cannot be cast to java.lang.Comparable”
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. MyClass没有实现Comparable。 I just want to use a Set to filter the unique elements of the List since my List contains unncessary duplicates. 我只想使用Set来过滤List的唯一元素,因为我的List包含不必要的重复项。
Does MyClass implements Comparable<MyClass>
or anything like that? MyClass implements Comparable<MyClass>
是否MyClass implements Comparable<MyClass>
或类似的东西?
If not, then that's why. 如果没有,那就是为什么。
For TreeSet
, you either have to make the elements Comparable
, or provide a Comparator
. 对于TreeSet
,您必须使元素Comparable
,或者提供一个Comparator
。 Otherwise TreeSet
can't function since it wouldn't know how to order the elements. 否则TreeSet
无法运行,因为它不知道如何排序元素。
Remember, TreeMap implements SortedSet
, so it has to know how to order the elements one way or another. 记住, TreeMap implements SortedSet
,因此它必须知道如何以一种或另一种方式对元素进行排序。
You should familiarize yourself with how implementing Comparable
defines natural ordering for objects of a given type. 您应该熟悉如何实现Comparable
为给定类型的对象定义自然顺序 。
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. 接口定义了一个方法compareTo
,如果该对象分别小于,等于或大于另一个对象,则该方法必须返回负整数,零或正整数。
The contract requires that: 合同要求 :
sgn(x.compareTo(y)) == -sgn(y.compareTo(x))
x.compareTo(y)>0 && y.compareTo(z)>0
implies x.compareTo(z)>0
它是可传递的: x.compareTo(y)>0 && y.compareTo(z)>0
表示x.compareTo(z)>0
x.compareTo(y)==0
implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z))
for all z
x.compareTo(y)==0
表示所有z
sgn(x.compareTo(z)) == sgn(y.compareTo(z))
Additionally, it recommends that: 此外,它建议 :
(x.compareTo(y)==0) == (x.equals(y))
, ie "consistent with equals
(x.compareTo(y)==0) == (x.equals(y))
,即“与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. 如果无法以一种或另一种方式对对象进行排序,则TreeSet
毫无意义。 You may want to use a HashSet
instead, which have its own contracts. 您可能要改用具有自己的合同的HashSet
。 You are likely to be required to @Override hashCode()
and equals(Object)
as appropriate for your type (see: Overriding equals and hashCode in Java ) 您可能需要根据您的类型使用@Override hashCode()
和equals(Object)
(请参阅: 在Java中覆盖equals和hashCode )
If you don't pass an explicit Comparator
to a TreeSet
, it will try to compare the objects (by assuming they are Comparable
). 如果您没有将显式的Comparator
传递给TreeSet
,它将尝试比较这些对象(假设它们是Comparable
)。 And if they aren't Comparable
, it cannot compare them, so this exception is thrown! 而且,如果它们不是Comparable
,则无法比较它们,因此将引发此异常!
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
. TreeSets
是排序的集合,需要将对象设为Comparable
或将Comparator
传入,以确定如何对Set
的对象进行排序。
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. 如果只想让集合删除重复项,则使用HashSet
,尽管这将以随机出现的方式乱序Iterator
返回的对象的顺序。
But if you want to preserve the order somewhat, use LinkedHashSet
, that will at least preserve the insertion order of the list. 但是,如果要保留顺序,请使用LinkedHashSet
,这将至少保留列表的插入顺序。
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. 仅当需要按对象的Comparable
实现或传递给TreeSet's
构造函数的自定义Comparator
对Set
排序时, TreeSet
才适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.