当前位置:网站首页>Map排序工具类
Map排序工具类
2022-06-28 11:38:00 【一杯苦芥】
public class MapUtil {
public static Map<String, String> sortByKey(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, String> sortMap = new TreeMap<>((str1, str2) -> str1.compareTo(str2));
sortMap.putAll(map);
return sortMap;
}
public static Map<String, String> sortByKeyIgnoreCase(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<String, String> sortMap = new TreeMap<>((str1, str2) -> str1.toLowerCase().compareTo(str2.toLowerCase()));
sortMap.putAll(map);
return sortMap;
}
public static Map<String, String> sortByValue(Map<String, String> oriMap) {
if (oriMap == null || oriMap.isEmpty()) {
return null;
}
Map<String, String> sortedMap = new LinkedHashMap<>();
List<Map.Entry<String, String>> entryList = new ArrayList<>(oriMap.entrySet());
Collections.sort(entryList, (me1, me2) -> me1.getValue().compareTo(me2.getValue()));
Iterator<Map.Entry<String, String>> iterator = entryList.iterator();
Map.Entry<String, String> tmpEntry;
while (iterator.hasNext()) {
tmpEntry = iterator.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
return sortedMap;
}
public static Map<String, String> sortByValueIgnoreCase(Map<String, String> oriMap) {
if (oriMap == null || oriMap.isEmpty()) {
return null;
}
Map<String, String> sortedMap = new LinkedHashMap<>();
List<Map.Entry<String, String>> entryList = new ArrayList<>(oriMap.entrySet());
Collections.sort(entryList, (me1, me2) -> me1.getValue().toLowerCase().compareTo(me2.getValue().toLowerCase()));
Iterator<Map.Entry<String, String>> iterator = entryList.iterator();
Map.Entry<String, String> tmpEntry;
while (iterator.hasNext()) {
tmpEntry = iterator.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
return sortedMap;
}
}
边栏推荐
- Graphics view framework for QT learning (to realize startup animation)
- Excel import / export convenience tool class
- Web page tips this site is unsafe solution
- 纯纯大怨种!那些年被劝退的考研专业
- Analyze whether there is duplicate data in the list and repeat it several times
- Is it safe to buy stocks and open an account on the account QR code of the CICC securities manager? Ask the great God for help
- day24 js笔记 2021.09.15
- day25 js中的预解析、递归函数、事件 2021.09.16
- AcWing 607. Average 2 (implemented in C language)
- Django -- MySQL database reflects the mapping data model to models
猜你喜欢

. Net hybrid development solution 24 webview2's superior advantages over cefsharp

js中的class类模式及语法 2021.11.10

Array method in JS 2021.09.18

Cannot redeclare block range variables

Day33 JS note event (Part 2) September 28, 2021

Packaging and publishing application of jetpack compose desktop version

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

js中的数组方法 2021.09.18

Redis principle - List

Is it feasible to be a programmer at the age of 26?
随机推荐
Django -- MySQL database reflects the mapping data model to models
day30 js笔记 BOM和DOM 2021.09.24
Day28 strict mode, string JS 2021.09.22
Leetcode 48. 旋转图像(可以,已解决)
【vi/vim】基本使用及命令汇总
面试步骤的面试技巧
MapReduce项目案例3——温度统计
Still using simpledateformat for time formatting? Be careful that the project collapses!
AcWing 606. Average 1 (implemented in C language)
开源项目维权成功案例: spug 开源运维平台成功维权
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
What is the main chain system?
Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
水果FL Studio/Cubase/Studio one音乐宿主软件对比
6.A-B
IO stream of file and Base64
【C语言】如何很好的实现复数类型
纯纯大怨种!那些年被劝退的考研专业
零基础C语言(一)
Day24 JS notes 2021.09.15