将map变成一个List,然后实现Comparator接口中的compare方法进行排序
Map<String, Integer> map = new HashMap<>();
map.put("zym9", 9);
map.put("zym6", 6);
map.put("zym-1", -1);
map.put("zym3", 3);
map.put("zym11", 11);
map.put("zym4", 4);
List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, (o1, o2) -> (o1.getValue() - o2.getValue()));
for (Map.Entry entry : entries) {
System.out.println(entry.getKey() + "--" + entry.getValue());
}
输出结果------------------------------------
zym-1&_&-1
zym3&_&3
zym4&_&4
zym6&_&6
zym9&_&9
zym11&_&11