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 ...
随机推荐
- C++ string类取字符串的左右子串(以特定子串为分界限)
// Example3.cpp : 定义控制台应用程序的入口点. //以特定单词为分界,求取字符串的左右子串 #include "StdAfx.h" #include <st ...
- NYOJ129 决策树 【并检查集合】
树的判定 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 A tree is a well-known data structure that is either e ...
- spring mvc综合easyui点击上面菜单栏中的菜单项问题
采用easyui的tree报错发生的背景后,会弹出一个窗口,有一个问题是,当你点击顶部 解决方案,如下面(运用easyui1.36): /home/cyz/workspace/hb_manager ...
- Net Memory Profiler 分析.Net程序内存泄露
Net Memory Profiler 分析.Net程序内存泄露 Haozes's Tech Space 人類的全部才能無非是時間和耐心的混合物 使用.Net Memory Profiler 分析.N ...
- WPF设置VistualBrush的Visual属性制作图片放大镜效果
原文:WPF设置VistualBrush的Visual属性制作图片放大镜效果 效果图片:原理:设置VistualBrush的Visual属性,利用它的Viewbox属性进行缩放. XAML代码:// ...
- Xamarin.Android 入门实例(3)之呼叫电话号码
1.Main.axml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- CopyOnWriteArrayList源代码阅读器
java.util.concurrent在相应的并发集合的包中定义的通用集合类,为了有效地处理并发场景.间CopyOnWriteArrayList它是合适ArrayList.顾名思义CopyOnWri ...
- c# 通过配置自动附加数据库
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Data.SqlCli ...
- [LeetCode203]Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 ...
- CSS3制作精美的iphone电话图标,不使用图片
<!DOCTYPE HTML> <html lang=zh-cn> <head> <meta charset=utf-8> <title>C ...