Map集合根据key,value排序
生活随笔
收集整理的這篇文章主要介紹了
Map集合根据key,value排序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/*** 根據(jù)map的key排序* * @param map 待排序的map* @param isDesc 是否降序,true:降序,false:升序* @return 排序好的map*/public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map, boolean isDesc) {Map<K, V> result = Maps.newLinkedHashMap();if (isDesc) {map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));} else {map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));}return result;}/*** 根據(jù)map的value排序* * @param map 待排序的map* @param isDesc 是否降序,true:降序,false:升序* @return 排序好的map*/public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean isDesc) {Map<K, V> result = Maps.newLinkedHashMap();if (isDesc) { map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue().reversed()).forEach(e -> result.put(e.getKey(), e.getValue()));} else { map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue()).forEachOrdered(e -> result.put(e.getKey(), e.getValue()));}return result;}
}
總結(jié)
以上是生活随笔為你收集整理的Map集合根据key,value排序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内涵跟不上颜值!Nothing Phon
- 下一篇: 数据结构 - 顺序存储二叉树(前序中序后