- Map<String, Integer> map = new TreeMap<String, Integer>();
- map.put("a", 100);
- map.put("c", 20);
- map.put("d", 20);
- map.put("b", 30);
- // 这里将map.entrySet()转换成list
- List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(
- map.entrySet());
- // 然后通过比较器来实现排序
- Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
- // 升序排序
- public int compare(Entry<String, Integer> o1,
- Entry<String, Integer> o2) {
- return o1.getValue().compareTo(o2.getValue());
- }
- });
- for (Map.Entry<String, Integer> mapping : list) {
- System.out.println(mapping.getKey() + ":" + mapping.getValue());
- }
- //该片段来自于http://www.codesnippet.cn/detail/1007201513077.html
来源: http://www.codesnippet.cn/detail/1007201513077.html