简体   繁体   English

java.lang.ClassCastException:排序时无法将java.lang.Short强制转换为java.lang.Integer

[英]java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Integer when sorting

How to sort hashmap by value if the values are Short type HashMap<Long, Short>() ? 如果值是Short类型HashMap<Long, Short>()如何按值对哈希图排序?

when I use generic sorting like 当我使用通用排序

public static <K extends Comparable<K>, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
    Comparator<K> valueComparator =  new Comparator<K>() {
        public int compare(K k1, K k2) {
            int compare = map.get(k1).compareTo(map.get(k2));
            return compare != 0 ? compare : k1.compareTo(k2);
        }
    };
    Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
    sortedByValues.putAll(map);
    return sortedByValues;
}

I get this error: 我收到此错误:

java.lang.ClassCastException: java.lang.Short cannot be cast to java.lang.Integer java.lang.ClassCastException:无法将java.lang.Short强制转换为java.lang.Integer

There are no explicit typecasts anywhere in this code. 此代码中的任何地方都没有显式的类型转换。

Therefore, I conclude that the ClassCastException is caused by the implicit typecasts that the compiler inserts when you assign a value returned by some generic method to a variable with a specific type. 因此,我得出的结论是, ClassCastException是由将某些通用方法返回的值分配给具有特定类型的变量时编译器插入的隐式类型转换引起的。

Furthermore, since the implicit typecasts only fail in circumstances where you have / someone has choosen to ignore or suppress Java compiler warnings about unsafe conversions, etcetera, I conclude that 此外,由于隐式类型转换仅在您/某人选择忽略或禁止有关不安全转换等的Java编译器警告的情况下失败,因此我得出结论:

  • this must have happened, and 这一定发生了,并且
  • it is the cause of your problem. 这是造成您问题的原因。

In short, the cause of problem is not in the code you have included in the question. 简而言之,问题的原因不在问题中包含的代码中。 It is somewhere else ... 在别的地方...


In future, be sure to include: 将来,请确保包括:

  • Sufficient context to allow people to see how your code is being used. 足够的上下文,使人们可以看到如何使用您的代码。 Ideally an MCVE. 理想情况下是MCVE。
  • The complete stacktrace for the exceptions. 异常的完整堆栈跟踪。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:无法强制转换java.lang.Integer - java.lang.ClassCastException: java.lang.Integer cannot be cast java.lang.ClassCastException:无法转换为java.lang.Integer - java.lang.ClassCastException: cannot be cast to java.lang.Integer java.lang.ClassCastException:java.lang.Long 不能在 java 1.6 中转换为 java.lang.Integer - java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer in java 1.6 java.lang.ClassCastException:无法在Tableau中将java.lang.String强制转换为java.lang.Integer? - java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer in tableau? java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long - java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long Hibernate:java.lang.ClassCastException:java.lang.Integer无法强制转换为java.lang.Double - Hibernate : java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double serverError:类java.lang.ClassCastException java.lang.Integer无法转换为java.lang.String - serverError: class java.lang.ClassCastException java.lang.Integer cannot be cast to java.lang.String Hiibernate:java.lang.ClassCastException:java.lang.String无法转换为java.lang.Integer - Hiibernate : java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer java.lang.ClassCastException:java.lang.Integer无法强制转换为java.lang.Double - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM