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; }
随笔看心情