Type mismatch: cannot convert from element type Object to Map.Entry<String,String>

Type mismatch: cannot convert from element type Object to Map.Entry<String,String>

出现这个错误我是把出错的地方涉及到的方法,全部用到的

参数类型要写完整

,就不报错了。这个错误在有的本地开发环境不会报错,但是做成接口供外部调用接口时就可能会失败了,所以,要养成所有类型写规范的习惯。

方法块示例:

 public static List<Entry<String, Long>> sortByValueAsc(TreeMap<String, Long> map) {
        List<Entry<String, Long>> list =
                new ArrayList<Entry<String, Long>>(map.entrySet());
        
        Collections.sort(list, new Comparator<Entry<String, Long>>() {
            @Override
            public int compare(Entry<String, Long> o1,
                    Entry<String, Long> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        return list;
    }

调用示例

 TreeMap map = new TreeMap<>();
        map.put("231", 3213L);
        map.put("41", 23231231L);
        map.put("24", 201L);
        map.put("2321", 321L);
        map.put("3213", 3L);
        for (Entry<String, Long> entry : MapSortUtil.sortByValueAsc(map).subList(2, 5)) {
            System.out.println(entry.getKey()+"------"+entry.getValue());
        }

因为map的类型没有写完整而报错,修改为:

 TreeMap<String, Long> map = new TreeMap<>();
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值