Java如何对HashMap按值进行排序--非String int 类型时

比如:Float  可以通过相减 取整返回,如下方:

 

 

Map<String, Float> PathMap = new HashMap<String, Float>();
//排序 map
//将map.entrySet()转换成list
List<Map.Entry<String, Float>> list = new ArrayList<Map.Entry<String, Float>>(PathMap.entrySet());
System.out.println("------- 转换前");
for (Map.Entry<String, Float> mapping : list) {
    System.out.println(mapping.getKey() + "\t" + mapping.getValue());
}

System.out.println("------- 转换后");
Collections.sort(list, new Comparator<Map.Entry<String, Float>>() {
    //降序排序
    public int compare(Map.Entry<String, Float> o1, Map.Entry<String, Float> o2) {
        //return o1.getValue().compareTo(o2.getValue());
        return  Math.round((o2.getValue()-o1.getValue())*100000);
    }
});

for (Map.Entry<String, Float> mapping : list) {
    System.out.println(mapping.getKey() + "\t" + mapping.getValue());
}

 

 

Java如何对HashMap按值进行排序_上善若水,水善利万物而不争。-CSDN博客
https://blog.csdn.net/xifeijian/article/details/46522531

 

完美解决Collections.sort对double不能排序问题_Deep Learning and NLP Farm-CSDN博客
https://blog.csdn.net/appleml/article/details/41315429

 

java.util.Arrays.sort(float[])方法实例 - Java.util包 | 速学堂教程 - 学的不仅是技术,更是梦想!--尚学堂旗下高端品牌
https://www.sxt.cn/util/arrays_sort_float.html

上一篇:910_AUTOSAR_TPS_SoftwareComponentTemplate19_端口注释2


下一篇:ksvcreate: Process(m000) creation failed 处理