add method

This commit is contained in:
Looly
2021-01-31 19:19:28 +08:00
parent 5ca45ded2b
commit 2d3d41c9f2
2 changed files with 21 additions and 0 deletions

View File

@@ -796,6 +796,26 @@ public class MapUtil {
return result;
}
/**
* 按照值排序,可选是否倒序
*
* @param map 需要对值排序的map
* @param <K> 键类型
* @param <V> 值类型
* @param isDesc 是否倒序
* @return Map<k, V> 排序后新的Map
* @since 5.5.8
*/
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean isDesc) {
Map<K, V> result = new LinkedHashMap<>();
Comparator<Entry<K, V>> entryComparator = Entry.comparingByValue();
if(isDesc){
entryComparator = entryComparator.reversed();
}
map.entrySet().stream().sorted(entryComparator).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
return result;
}
/**
* 创建代理Map<br>
* {@link MapProxy}对Map做一次包装提供各种getXXX方法