map进行value排序

public static Map<String, Long> sortMap(Map<String, Long> map) {
        List<Map.Entry<String, Long>> entryList = new ArrayList<>(map.entrySet());
        entryList.sort((o1, o2) -> {
            int v1 = o1.getValue() == null ? 0 : o1.getValue().intValue();
            int v2 = o2.getValue() == null ? 0 : o2.getValue().intValue();
            // 倒序
            return v2 - v1;
        });
        LinkedHashMap<String, Long> linkedHashMap = new LinkedHashMap<String, Long>();
        for (Map.Entry<String, Long> e : entryList) {
            linkedHashMap.put(e.getKey(), e.getValue());
        }
        return linkedHashMap;
    }

 

posted @ 2023-02-21 09:56  二次元的程序猿  阅读(46)  评论(0编辑  收藏  举报