TreeMap cannot be cast to java.lang.Comparable

Posted byte01

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TreeMap cannot be cast to java.lang.Comparable相关的知识,希望对你有一定的参考价值。

 

TreeMap

 

/**
* Constructs a new, empty tree map, using the natural ordering of its
* keys. All keys inserted into the map must implement the @link
* Comparable interface. Furthermore, all such keys must be
* <i>mutually comparable</i>: <tt>k1.compareTo(k2)</tt> must not throw
* a <tt>ClassCastException</tt> for any keys <tt>k1</tt> and
* <tt>k2</tt> in the map. If the user attempts to put a key into the
* map that violates this constraint (for example, the user attempts to
* put a string key into a map whose keys are integers), the
* <tt>put(Object key, Object value)</tt> call will throw a
* <tt>ClassCastException</tt>.
*/
public TreeMap()
comparator = null;

 

 

/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
*
* @return * @throws ClassCastException if the specified key cannot be compared
* with the keys currently in the map@throws NullPointerException if the specified key is null
* and this map uses natural ordering, or its comparator
* does not permit null keys
*/
public V put(K key, V value)
Entry<K,V> t = root;
if (t == null)
// TBD:
// 5045147: (coll) Adding null to an empty TreeSet should
// throw NullPointerException
//
// compare(key, key); // type check
root = new Entry<K,V>(key, value, null);
size = 1;
modCount++;
return null;

int cmp;
Entry<K,V> parent;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator;
if (cpr != null)
do
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
while (t != null);

else
if (key == null)
throw new Comparable<? super K> k = (Comparable<? super K>) key;
do
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
while (t != null);

Entry<K,V> e = new Entry<K,V>(key, value, parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
return null;

 

一个报错的场景:

package map;

import java.util.Map;
import java.util.TreeMap;

public class TreeMapDemo
public static void main(String[] args)
Map<Long, Staff> treeMap = new TreeMap<Long, Staff>();
for (int i = 0; i < 10; i++)
treeMap.put(i + Math.round(Math.random() * 10), new Staff(i));

System.out.println(treeMap);
System.out.println("result:" + treeMap.containsKey(10));
System.out.println("result:" + treeMap.containsKey(1l));




class Staff
private int yearOfWorking;

public Staff(int yearOfWorking)
this.yearOfWorking = yearOfWorking;


@Override
public String toString()
return "Staff" +
"yearOfWorking=" + yearOfWorking +
;

5=StaffyearOfWorking=1, 7=StaffyearOfWorking=5, 9=StaffyearOfWorking=7, 10=StaffyearOfWorking=9, 11=StaffyearOfWorking=2, 13=StaffyearOfWorking=8
Exception in thread "main"java.lang.Long cannot be cast to java.lang.Integer35)
at java.util.TreeMap.getEntry(TreeMap.java:328)
at java.util.TreeMap.containsKey(TreeMap.java:209)
at map.TreeMapDemo.main(TreeMapDemo.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

 


 



以上是关于TreeMap cannot be cast to java.lang.Comparable的主要内容,如果未能解决你的问题,请参考以下文章

cannot be cast to javax.servletFilter

cannot be cast to javax.servletFilter

为啥会出现cannot be cast to java.lang.String

cannot be cast to android.support.v4.app.Fragment

com.alibaba.fastjson.JSONArray cannot be cast to java.lang.String

[B cannot be cast to java.lang.String