博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TreeMap cannot be cast to java.lang.Comparable
阅读量:5770 次
发布时间:2019-06-18

本文共 4835 字,大约阅读时间需要 16 分钟。

 

 

/**     * 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 * mutually comparable: k1.compareTo(k2) must not throw * a ClassCastException for any keys k1 and * k2 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 * put(Object key, Object value) call will throw a * ClassCastException. */ 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 key, or     *         null if there was no mapping for key.     *         (A null return can also indicate that the map     *         previously associated null with key.)     * @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
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
(key, value, null); size = 1; modCount++; return null; } int cmp; Entry
parent; // split comparator and comparable paths Comparator
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
k = (Comparable
) 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
e = new Entry
(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
treeMap = new TreeMap
(); 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)

 

 

转载于:https://www.cnblogs.com/softidea/p/4917122.html

你可能感兴趣的文章
XP 安装ORACLE
查看>>
八、 vSphere 6.7 U1(八):分布式交换机配置(vMotion迁移网段)
查看>>
[转载] 中华典故故事(孙刚)——19 万岁
查看>>
php5编译安装常见错误和解决办法集锦
查看>>
Unable to determine local host from URL REPOSITORY_URL=http://
查看>>
ORACLE配置,修改tnsnames.ora文件实例
查看>>
Workstation服务无法启动导致无法访问文件服务器
查看>>
Linux常用命令(一)
查看>>
一个自动布署.net网站的bat批处理实例
查看>>
我的友情链接
查看>>
Centos6.6安装选包及基础场景说明
查看>>
java基础面试题-1
查看>>
lamp+nginx代理+discuz+wordpress+phpmyadmin搭建一
查看>>
windows server 2016 活动目录(二)
查看>>
openstack G版 修改vm的flavor级别
查看>>
python_控制台输出带颜色的文字方法
查看>>
Android组件化最佳实践 ARetrofit原理
查看>>
舍弃浮躁, 50条重要的C++学习建议
查看>>
同步手绘板——将View的内容映射成Bitmap转图片导出
查看>>
陌陌和请吃饭之类的应用,你要是能玩转,那就厉害了
查看>>