简体   繁体   English

插入TreeSet时发生ClassCastException

[英]ClassCastException when inserting into a TreeSet

Having the following Java TreeSet 具有以下Java TreeSet

TreeSet<Widget> tabs = new TreeSet<Widget>();

When I do tabs.add(new Widget()); 当我做tabs.add(new Widget()); in method a() it worked. 在方法a()中它起作用了。 When I do the same in method b() shortly after the first insert is performed I the a ClassCastException. 当我在第一次执行插入后不久在方法b()中执行相同的操作时,我发生了ClassCastException。

Change 更改

ArrayList<Widget> tabs = new ArrayList<Widget>();

It works fine in both methods a() and b() without any error. 它在方法a()和b()中都可以正常工作,没有任何错误。 I was wondering why there is a ClassCastException in the first example. 我想知道为什么第一个示例中存在ClassCastException。 There is no casting in place. 没有适当的转换。

Edit: Here are method a, b and the Widget class: 编辑:这是方法a,b和Widget类:

 private void a() {
   tabs.add(new Widget());
 }

 private void b() {
   tabs.add(new Widget());
 }

public class Widget() {}

How do I make the first example to work? 我如何使第一个示例起作用?

When you add to a TreeSet , the object you pass must either implement Comparable or you must specify a Comparator at the time the TreeSet is constructed. 当添加到TreeSet ,传递的对象必须实现Comparable 或者必须在构造TreeSet时指定Comparator

From the JavaDoc : JavaDoc

The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used. 元素使用其自然顺序进行排序,或通过在设置创建时提供的Comparator排序,具体取决于所使用的构造函数。

And the JavaDoc for TreeSet.add(E) even tells you why this happens... 而且TreeSet.add(E)JavaDoc甚至告诉您为什么会发生这种情况...

ClassCastException - if the specified object cannot be compared with the elements currently in this set ClassCastException- 如果无法将指定对象与此集合中当前的元素进行比较

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

相关问题 尽管实现了与compareTo方法相当的效果,但当Treeset.add()时发生ClassCastException - ClassCastException when Treeset.add(), despite implementing comparable with compareTo method 调用TreeSet时发生ClassCastException <Long> .contains(Long.valueOf(someLongValue)) - ClassCastException when calling TreeSet<Long>.contains( Long.valueOf( someLongValue ) ) 尝试在没有比较器的情况下添加树集时,StringBuffer 不会给出 ClassCastException - StringBuffer not giving ClassCastException when trying to add in a treeset without Comparator TreeSet引发ClassCastException吗? - TreeSet raises ClassCastException? 为什么TreeSet会抛出ClassCastException? - Why does TreeSet throw a ClassCastException? 转换List时ClassCastException <Map<String,Double> &gt;到TreeSet <Map<String,Double> &gt; - ClassCastException when converting List<Map<String,Double>> to TreeSet<Map<String,Double>> 当removeAll()时TreeSet中的NullPointerException - NullPointerException in TreeSet when removeAll() 添加到TreeSet时记录位置 - Recording location when adding to a TreeSet 当 compareto 返回 0 时了解 TreeSet - Understanding TreeSet when compareto returns 0 列表到TreeSet的转换产生:“ java.lang.ClassCastException:MyClass无法转换为java.lang.Comparable” - List to TreeSet conversion produces: “java.lang.ClassCastException: MyClass cannot be cast to java.lang.Comparable”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM