Map可根据value的值进行排序
代码块
代码块语法遵循标准markdown代码,例如:
map根据value的值进行排序。
List<Map.Entry<String, Integer>> list_Data = new
ArrayList<Map.Entry<String,Integer>>((Collection<?extends Entry<String, Integer>>) map.entrySet());
Collections.sort(list_Data,new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> map1, Entry<String,Integer> map2) {
return (map2.getValue() - map1.getValue());
}
});
降序或者升序只要改变return 中map1、map2的位置即可。
本文介绍了一种使用Java对Map集合中的元素按照其值进行排序的方法。通过将Map转换为List并利用Collections.sort方法配合自定义Comparator实现降序或升序排列。
806

被折叠的 条评论
为什么被折叠?



