TreeMap cannot be cast to java.lang.Comparable

/**
* 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 the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>.)
* @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 NullPointerException();
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=Staff{yearOfWorking=1}, 7=Staff{yearOfWorking=5}, 9=Staff{yearOfWorking=7}, 10=Staff{yearOfWorking=9}, 11=Staff{yearOfWorking=2}, 13=Staff{yearOfWorking=8}}
Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
at java.lang.Integer.compareTo(Integer.java:35)
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 java.lang.Comparable异常
Set集合中的treeSet问题:cannot be cast to java.lang.Comparable: 原理: Set不保存重复的元素,与Collection类似,只是行为不同,Set是基于 ...
- cannot be cast to java.lang.Comparable
Exception in thread "main" java.lang.ClassCastException: com.myradio.People cannot be cast ...
- Set集合中的treeSet问题:cannot be cast to java.lang.Comparable;
使用TreeSet保存自定义对象时, 必须让定义对象的类实现Comparable接口,并重写compareTo()方法 否则报 实体类User:cannot be cast to java.lang. ...
- java.lang.Long cannot be cast to java.lang.Integer解决办法
情景: mybatis连接oracle 报错: 测试增的时候,报错 Java.lang.Long cannot be cast to java.lang.Integer:删改没有报错. 排查过程: ...
- [记录]java.math.biginteger cannot be cast to java.lang.long
可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...
- java.lang.Comparable接口
转自:http://blog.csdn.net/zccst/article/details/5092920 java.lang.Comparable 接口 作者: zccst java.lang.Co ...
- 利用泛型抽取Dao层,加事务注解问题(java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType)
想利用泛型抽取BaseDao层,简化操作时出现故障: @Transactional这个注解是能够继承的.于是就想写在抽取的BaseDao层上,让实现的类能够不用写@Transactional,就可开启 ...
- Mybatis的失误填坑-java.lang.Integer cannot be cast to java.lang.String
Mybatis的CRUD小Demo 为方便查看每次的增删改结果,封装了查询,用来显示数据库的记录: public static void showInfo(){ SqlSession session ...
- Java.lang.Comparable接口和Java.util.Comparator接口的区别
Java的Comparator和Comparable当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序. 1.Com ...
随机推荐
- Gradle第二步骤来创建学习Task
请下载本系列中的以下文章Github演示示例代码: git clone https://github.com/davenkin/gradle-learning.git Gradle的Proje ...
- 水声通信(传声)于iOS、Android在情景-depth分析(包括一些声通信源)
最近的水声通信非常热,特别是,非常嵌入式设备备受瞩目使用,前段时间公布了声通信部分源代码(iOS和Android版本号.下载源的最新版本:点击打开链接 http://download.csdn.net ...
- String类疑虑
Java String类疑虑: 创 拼接 String是java的经常使用类之中的一个,本质是字符数组char[].String类是final类,不可被继承. 关于String的创建,能够通过new来 ...
- Java代码检查工具
FindBugs:不注重样式和格式,重视真正的缺陷和潜在的性能问题,,基于Bug Patterns,在不运行的情况下检查字节码代码质量. Jalopy:Java源代码格式化工具,可自定义规则. PMD ...
- 我见过最好的vsftpd配置教程(转)
环境:CentOS 5.0 操作系统一.安装:1.安装Vsftpd服务相关部件:[root@KcentOS5 ~]# yum install vsftpd*Dependencies Resolved= ...
- 解决Nginx的connect() to 127.0.0.1:8080 failed (13: Permission denied) while connect
在进行Nginx+Tomcat 负载均衡的时候遇到了这个权限问题,在error.log日志中.我们能够看到例如以下: connect() to 127.0.0.1:8080 failed (13: P ...
- MATLAB描绘极坐标图像——polar
polar可用于描绘极坐标图像. 最简单而经常使用的命令格式:POLAR(THETA, RHO) 当中,THETA是用弧度制表示的角度,RHO是相应的半径. 例: a=-2*pi:.001:2*pi ...
- Vc6.0打开该文件坠毁
这是一个微软bug,下面是用户给出的溶液: http://blog.163.com/wjatnx@yeah/blog/static/12758622820138110530322/ http://bl ...
- php 两个文件之间的相对路径的计算方法
php 两个文件之间的相对路径的计算方法 比如: 文件A 的路径是 /home/web/lib/img/cache.php 文件B的路径是 /home/web/api/img/show.php 那么. ...
- system strategies of Resources Deadlock
In computer science, Deadlock is a naughty boy aroused by compete for resources. Even now, there ...