按值对Map 进行排序

按值对Map 进行排序,第1张

按值对Map 进行排序

这是通用版本

public class MapUtil {    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {        List<Entry<K, V>> list = new ArrayList<>(map.entrySet());        list.sort(Entry.comparingByValue());        Map<K, V> result = new linkedHashMap<>();        for (Entry<K, V> entry : list) { result.put(entry.getKey(), entry.getValue());        }        return result;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5586650.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存