[英]What causes the ClassCastException: java.util.TreeSet cannot be cast to java.lang.Comparable?
So I'm trying to move all Strings of a certain length from a Collection of Strings (could either be a Set or a List) to a TreeMap and setting a Set of the characters in each String as the key for that String but the line map.put(keyRinger(word), word);
因此,我尝试将一定长度的所有字符串从字符串集合(可以是集合或列表)移动到TreeMap,并将每个字符串中的字符集设置为该字符串的键,但该行
map.put(keyRinger(word), word);
throws java.lang.ClassCastException: java.util.TreeSet cannot be cast to java.lang.Comparable
引发
java.lang.ClassCastException: java.util.TreeSet cannot be cast to java.lang.Comparable
Map<Set<Character>, String> map = new TreeMap<Set<Character>, String>();
for (String words : words) {
if (word.length() == length) {
map.put(keyRinger(word), word);
}
}
This is the keyRing
method in case you're curious. 如果您好奇的话,这是
keyRing
方法。
private Set<Character> keyRinger(String current) {
Set<Character> keyRing = new TreeSet<Character>();
for (int i = 0; i < current.length(); i++) {
char key = current.charAt(i);
keyRing.add(key);
}
return keyRing;
}
So my question is what can I do to avoid this? 所以我的问题是如何避免这种情况? I've read I need a
Comparator
or to implement Comparable
but I don't know how to do that, and I think there might be a simpler solution (although perhaps not as efficient). 我已经读过我需要
Comparator
或实现Comparable
但我不知道该怎么做,而且我认为可能有一个更简单的解决方案(尽管可能效率不高)。
You'll notice the javadoc of TreeMap
states 您会注意到
TreeMap
状态的javadoc
The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.
根据映射键的自然顺序或在映射创建时提供的Comparator对映射进行排序,具体取决于所使用的构造函数。
So if you don't provide a Comparator
, it will use natural ordering . 因此,如果您不提供
Comparator
,它将使用自然排序 。 What is natural ordering? 什么是自然排序? The javadoc of the
Comparable
interface states Comparable
接口状态的javadoc
This interface imposes a total ordering on the objects of each class that implements it.
该接口对实现该接口的每个类的对象强加了总体排序。 This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
此排序称为类的自然排序,而该类的compareTo方法被称为其自然比较方法。
So your key element in your TreeMap
must implement Comparable
. 因此,
TreeMap
关键元素必须实现Comparable
。 You are trying to use TreeSet
instances as keys in your TreeMap
. 您试图将
TreeSet
实例用作TreeMap
键。 TreeSet
does not implement Comparable
. TreeSet
没有实现Comparable
。
You get a ClassCastException
when TreeMap
tries to cast the key to a Comparable
reference in order to use its compareTo
method. 当
TreeMap
尝试将键Comparable
转换为Comparable
引用以便使用其compareTo
方法时,将收到ClassCastException
。
To correct this issue, you should create a TreeMap
by providing your own custom Comparator
for comparing Set<Character>
instances. 若要更正此问题,应通过提供自己的自定义
Comparator
来比较Set<Character>
实例来创建TreeMap
。
Here's how you can create the comparator class: 这是创建比较器类的方法:
public class NutrientByInclusionOrderComparator implements Comparator<ProductNutrient>{
@Override
public int compare(ProductNutrient o1, ProductNutrient o2) {
if (o1 == null && o2 == null){
return 0;
}
else if (o1 == null){
return -1;
}
else if (o2 == null){
return 1;
}
else if ( o1.getNumOrder().compareTo(o2.getNumOrder()) == 0) {
return o1.getDtInclusion().compareTo(o2.getDtInclusion());
} else {
return o1.getNumOrder().compareTo(o2.getNumOrder());
}
}
}
And then, when creating your TreeSet: 然后,在创建TreeSet时:
Set<ProductNutrient> productNutrients = new TreeSet<ProductNutrient>(new NutrientByInclusionOrderComparator());
Cheers! 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.