永发信息网

java如何对map集合进行值valuie的倒排序,出现值相同的再健key的倒排序,最后keyvalue组合输出

答案:1  悬赏:70  手机版
解决时间 2021-03-08 15:42
  • 提问者网友:呐年旧曙光
  • 2021-03-07 19:56
java如何对map集合进行值valuie的倒排序,出现值相同的再健key的倒排序,最后keyvalue组合输出
最佳答案
  • 五星知识达人网友:青灯有味
  • 2021-03-07 21:21
public static void main(String[] args) {
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
map.put(1, 11);
map.put(2, 44);
map.put(3, 55);
map.put(4, 21);
map.put(5, 33);
map.put(6, 11);
//这里将map.entrySet()转换成list
List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
//然后通过比较器来实现排序
Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
//升序排序
public int compare(Entry<Integer, Integer> o1,Entry<Integer, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}

});

for(Map.Entry<Integer,Integer> mapping:list){
System.out.println(mapping.getKey()+":"+mapping.getValue());
}
}
这是根据value的值排序的,你也可以泛型成Map<String,String>一样的 希望能帮到你!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯