zoukankan      html  css  js  c++  java
  • 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)





  • 相关阅读:
    java lambda表达式学习笔记
    一个奇妙的java坑:Long 类型的比较
    要不要冗余字段
    mysql找回密码
    mysql创建触发器
    【转】Java 8十个lambda表达式案例
    ThreadLocal类学习笔记
    mybatis generator使用总结
    【转】Java 项目UML反向工程转化工具
    PostMan做接口自动化测试
  • 原文地址:https://www.cnblogs.com/softidea/p/4917122.html
Copyright © 2011-2022 走看看