Java8使用Stream对Map排序
HashMap<String, Long> map = new HashMap<>(); map.put("a", 10L); map.put("b", 20L); map.put("c", 15L); map.put("d", 23L); System.out.println("----Before Sort-----"); map.forEach((k, v) -> { System.out.println(k + " = " + v); }); Map<String, Long> finalMap = new LinkedHashMap<>(); map.entrySet().stream() .sorted(Map.Entry.<String, Long> comparingByValue().reversed()) .forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue())); System.out.println("----After Sort-----"); finalMap.forEach((k, v) -> { System.out.println(k + " = " + v); }); 来源: oschina 链接: https://my.oschina.net/u/575679/blog