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

@ -24,6 +24,7 @@
* 【db 】 ThreadUtil增加createScheduledExecutor和schedule方法issue#I2NUTC@Gitee
* 【core 】 ImgUtil增加getImage方法issue#I2DU1Z@Gitee
* 【core 】 DateUtil.beginOfHourpr#269@Gitee
* 【core 】 MapUtil增加sortByValuepr#259@Gitee
### Bug修复
* 【core 】 修复FileUtil.move以及PathUtil.copy等无法自动创建父目录的问题issue#I2CKTI@Gitee

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方法