0
点赞
收藏
分享

微信扫一扫

Stream排序Map集合

肉肉七七 2022-03-23 阅读 73
java后端

前置知识: Lamdba表达式 StreamApi

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

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

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

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

在实际开发中经常会用到HashMap的排序虽然可以用TreeMap排序 但是只可以对键进行排序 用Stream可以极大提高开发效率感觉有用就点个赞吧 如果有错误或更好的方法评论区请多多指出 相互学习共同进步 谢谢

举报

相关推荐

0 条评论