所以我遇到了这种能够按值对HashMaps进行排序的方法.
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
return map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(e1, e2) -> e1,
LinkedHashMap::new
));
}
Run Code Online (Sandbox Code Playgroud)
我想使用该reversed()
方法,Comparator
但我似乎找不到合适的地方.