java8 Stream Map集合按value值排序
Map<String, Integer> map = new HashMap<>();
map.put("a",1);
map.put("d",4);
map.put("c",3);
map.put("e",5);
map.put("b",2);
List<Integer> list = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) //降序
//.sorted(Map.Entry.comparingByValue()) //升序
.map(Map.Entry::getValue)
.collect(Collectors.toList());
System.out.println(list.toString());
结果:
[5, 4, 3, 2, 1]