简体   繁体   English

列表到TreeSet的转换产生:“ java.lang.ClassCastException:MyClass无法转换为java.lang.Comparable”

[英]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))
  • it's transitive: 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构造函数的自定义ComparatorSet排序时, TreeSet才适用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MyClass无法强制转换为java.lang.Comparable:java.lang.ClassCastException - MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException java.lang.ClassCastException: 不能转换为 java.lang.Comparable - java.lang.ClassCastException: cannot be cast to java.lang.Comparable java.lang.ClassCastException:java.util.HashMap无法强制转换为java.lang.Comparable - java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable java.lang.ClassCastException:尝试排序List时,java.util.LinkedHashMap无法转换为java.lang.Comparable异常 - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.Comparable exception when trying sort a List &#39;java.lang.ClassCastException:资源无法强制转换为java.lang.Comparable&#39; - 'java.lang.ClassCastException: Resource cannot be cast to java.lang.Comparable' 线程“主”java.lang.ClassCastException 中的异常:setcollection.Enseignant 无法转换为 java.lang.Comparable - Exception in thread “main” java.lang.ClassCastException: setcollection.Enseignant cannot be cast to java.lang.Comparable java.lang.ClassCastException:com.MyComp.model.Image无法转换为java.lang.Comparable - java.lang.ClassCastException: com.MyComp.model.Image cannot be cast to java.lang.Comparable java.lang.ClassCastException:jdk.nashorn.internal.objects.NativeArray无法转换为java.lang.Comparable - java.lang.ClassCastException: jdk.nashorn.internal.objects.NativeArray cannot be cast to java.lang.Comparable 线程“主”java.lang.ClassCastException 中的异常:proj.Car 无法转换为 java.lang.Comparable - Exception in thread "main" java.lang.ClassCastException: proj.Car cannot be cast to java.lang.Comparable 是什么原因导致ClassCastException:java.util.TreeSet无法转换为java.lang.Comparable? - What causes the ClassCastException: java.util.TreeSet cannot be cast to java.lang.Comparable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM