java lamda map升序

    // 升序
    public static <K, V extends Comparable<? super V>> Map<K, V> mapSortAsc(Map<K, V> map) {
        return map.entrySet().stream().sorted((o1, o2) -> o1.getValue().compareTo(o2.getValue())).map(entry -> {
            Map<K, V> result = new LinkedHashMap<>();
            result.put(entry.getKey(), entry.getValue());
            return result;
        }).reduce((map1, map2) -> {
            map2.entrySet().forEach(entry -> map1.put(entry.getKey(), entry.getValue()));
            return map1;
        }).get();
    }
发布了65 篇原创文章 · 获赞 38 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/HelloWorldYangSong/article/details/103313865
今日推荐