简体   繁体   English

是什么原因导致ClassCastException:java.util.TreeSet无法转换为java.lang.Comparable?

[英]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.

相关问题 列表到TreeSet的转换产生:“ java.lang.ClassCastException:MyClass无法转换为java.lang.Comparable” - List to TreeSet conversion produces: “java.lang.ClassCastException: MyClass 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: 不能转换为 java.lang.Comparable - java.lang.ClassCastException: cannot be cast to java.lang.Comparable java.util.ArrayList无法转换为java.lang.Comparable - java.util.ArrayList 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 java.lang.ClassCastException: org.postgresql.util.PGobject 不能转换为 java.lang.Comparable; UUID转换器内部 - java.lang.ClassCastException: org.postgresql.util.PGobject cannot be cast to java.lang.Comparable; inside UUIDconverter 线程“主”java.lang.ClassCastException 中的异常:javafx.util.Pair 无法转换为 java.lang.Comparable - Exception in thread “main” java.lang.ClassCastException: javafx.util.Pair cannot be cast to java.lang.Comparable 不能转换为 java.lang.Comparable - cannot be cast to java.lang.Comparable MyClass无法强制转换为java.lang.Comparable:java.lang.ClassCastException - MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException &#39;java.lang.ClassCastException:资源无法强制转换为java.lang.Comparable&#39; - 'java.lang.ClassCastException: Resource cannot be cast to java.lang.Comparable'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM