HashMap 使用lamda表达式排序

map.forEach((key, value) -> System.out.println("key:" + key + ",value:"+ value));
        System.out.println("-------");
        HashMap res = map.entrySet().stream().sorted((e1, e2) -> compare(e1, e2))
                .collect(toMap(Map.Entry :: getKey, Map.Entry :: getValue, (oldvalue, newvalue) -> oldvalue, LinkedHashMap :: new));
        res.forEach((key,value) -> System.out.println("key:"+ key + ",value:" + value));
public static int compare(Map.Entry<Character,Integer>e1,Map.Entry<Character,Integer>e2){

        int re = e2.getValue().compareTo(e1.getValue());

        if(re!=0){return re;}

        else{return e1.getKey().compareTo(e2.getKey());}

    }
原文地址:https://www.cnblogs.com/leavescy/p/14442768.html