zoukankan      html  css  js  c++  java
  • Map按照数值进行排序

    public static Map<String, Integer> sortMapByValue(Map<String, Integer> oriMap) {
            if (oriMap == null || oriMap.isEmpty()) {
                return null;
            }
            Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
            List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(
                    oriMap.entrySet());
            Collections.sort(entryList, new MapValueComparator());
    
            Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
            Map.Entry<String, Integer> tmpEntry = null;
            while (iter.hasNext()) {
                tmpEntry = iter.next();
                sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
            }
            return sortedMap;
        }
    class MapValueComparator implements Comparator<Map.Entry<String, Integer>> {
    
        @Override
        public int compare(Entry<String, Integer> me1, Entry<String, Integer> me2) {
    
            return me2.getValue().compareTo(me1.getValue());
        }
    }
  • 相关阅读:
    uva11552
    zoj3820 树的直径+二分
    hdu 5068 线段树加+dp
    zoj3822
    uva1424
    DAY 36 前端学习
    DAY 35 前端学习
    DAY 34 PYTHON入门
    DAY 33 PYTHON入门
    DAY 32 PYTHON入门
  • 原文地址:https://www.cnblogs.com/plain-heart/p/7560213.html
Copyright © 2011-2022 走看看