排序map

1、根据map的值,升序排序

 1 Map<String, Integer> map = new TreeMap<String, Integer>();
 2 map.put("d", 1);
 3 map.put("b", 2);
 4 map.put("a", 3);
 5 map.put("c", 4);
 6 
 7 // 这里将map.entrySet()转换成list
 8 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
 9 // 然后通过比较器来实现排序
10 Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
11     // 升序排序
12     public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
13         return o1.getValue().compareTo(o2.getValue());
14     }
15 
16 });
17 for (Map.Entry<String, Integer> mapping : list) {
18     System.out.println(mapping.getKey() + ":" + mapping.getValue());
19 }
View Code
这个博客主要是javaEE相关或者不相关的记录, hadoop与spark的相关文章我写在下面地址的博客啦~ http://www.cnblogs.com/sorco
原文地址:https://www.cnblogs.com/orco/p/6233589.html