是什么原因导致ClassCastException:java.util.TreeSet不能被转换为java.lang.Comparable? [英] What causes the ClassCastException: java.util.TreeSet cannot be cast to java.lang.Comparable?

查看:629
本文介绍了是什么原因导致ClassCastException:java.util.TreeSet不能被转换为java.lang.Comparable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图将一个特定长度的字符串从一个字符串集合(可以是一个集合或一个列表)移动到一个TreeMap,并设置每个字符串中的字符集作为该字符串的键
,但 map.put(keyRinger(word),word); throws java.lang.ClassCastException:java.util.TreeSet不能转换为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); throws 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);
  }
}

这是 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;
}

所以我的问题是,我看过我需要一个 Comparator 或实现 Comparable ,但我不知道该怎么做,我认为可能有一个更简单的解决方案(虽然可能效率不高)。

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).

推荐答案

href =http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html =nofollow> TreeMap states

You'll notice the javadoc of TreeMap states


地图根据其键的自然顺序排序,或按
a在映射创建时提供的比较器

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 ,它将使用自然排序。什么是自然排序? 可比较的/ code> 接口状态

So if you don't provide a Comparator, it will use natural ordering. What is natural ordering? The javadoc of the Comparable interface states


此接口对每个类
的对象施加总排序实现它。 这种顺序被称为类的
自然排序
,类的compareTo方法被称为
的自然比较方法。

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.

因此,您的 TreeMap 中的关键元素必须实现 Comparable 。您尝试使用 TreeSet 实例作为 TreeMap 中的键。 TreeSet 不会实现 Comparable

So your key element in your TreeMap must implement Comparable. You are trying to use TreeSet instances as keys in your TreeMap. TreeSet does not implement Comparable.

您会得到 ClassCastException 尝试将键转换为 Comparable 引用以便使用 $ c> compareTo 方法。

You get a ClassCastException when TreeMap tries to cast the key to a Comparable reference in order to use its compareTo method.

要更正此问题,您应该通过提供自己的自定义比较器来创建 TreeMap 用于比较设置< Character> 实例。

To correct this issue, you should create a TreeMap by providing your own custom Comparator for comparing Set<Character> instances.

这篇关于是什么原因导致ClassCastException:java.util.TreeSet不能被转换为java.lang.Comparable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆