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; }
标签:map,Map,entryList,value,getValue,排序,o2,o1 From: https://www.cnblogs.com/stromgao/p/17139927.html