map集合排序

Posted by lizhikang on 2019-06-12

stream的map排序

map根据key正序排序

1
2
map.entrySet().stream().sorted(Comparator.comparing(e->e.getKey()))
.forEach(System.out::println);

map根据key倒序排序

1
2
map.entrySet().stream().sorted(Collections.reverseOrder
(Map.Entry.comparingByKey())).forEach(System.out::println);

map根据value正序排序

1
2
map.entrySet().stream().sorted(Comparator.comparing(e->e.getValue()))
.forEach(System.out::println);

map根据value倒序排序

1
2
map.entrySet().stream().sorted(Collections.reverseOrder
(Map.Entry.comparingByValue())).forEach(System.out::println);

本站总访问量 | 本站访客数