//升序
total.sort(Comparator.comparingInt(o -> MapUtils.getInteger(o, "total", 0)));
//降序
total.sort(Comparator.comparingInt(o -> MapUtils.getInteger(o, "total", 0)).reversed());
想要把升序改为降序后,报这个错误:
‘getInteger(java.util.Map<? super K,?>, K, java.lang.Integer)’ in ‘org.apache.commons.collections4.MapUtils’ cannot be applied to ‘(java.lang.Object, java.lang.String, int)’
错误信息表明 MapUtils.getInteger 方法的参数类型不匹配。问题在于 MapUtils.getInteger 方法需要一个具有通配符类型的 Map 对象,而不是 java.lang.Object。
这里的 MapUtils.getInteger 方法参数类型需要 Map<String, ?>,所以我们在调用时进行了类型转换 (Map<String, ?>)。这样可以确保类型匹配并避免编译错误。运行这段代码后,列表中的元素将按 “total” 键的值从大到小排序。