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

1、根据key排序

Map result = new HashMap<>();

Map map = new HashMap<>();

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

2、根据value排序

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

 

 

 

你可能感兴趣的:(java)