java8 map根据key排序和根据value排序

1、根据key排序

Map<String,String> result = new HashMap<>();

Map<String,String> map = new HashMap<>();

map.entrySet().stream()
    .sorted(Map.Entry.comparingByKey())
        .forEachOrdered(x->result.put(x.getKey(),x.getValue()));

2、根据value排序

Map<String, Integer> valueResult = new HashMap<>();
Map<String, Integer> map = new HashMap<>();
map.entrySet().stream()
    .sorted(Map.Entry
        .comparingByValue())
        .forEachOrdered(b->valueResult.put(b.getKey(), b.getValue()));
		

 

 

 

    原文作者:System.out.print
    原文地址: https://blog.csdn.net/weixin_40841731/article/details/89490560
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞