【发布时间】:2017-07-20 23:26:03
【问题描述】:
所以我遇到了这种能够按值对 HashMap 进行排序的方法。
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
));
}
我想在Comparator 上使用reversed() 方法,但我似乎找不到合适的放置位置。
【问题讨论】:
-
@JornVernee 不起作用
-
“不起作用”? “不起作用”怎么办?
-
@LewBloch .sorted(Map.Entry.comparingByValue().reversed()) 无法编译。 Mureinik 的解决方案有效
标签: java sorting generics hashmap comparator