当前位置:网站首页>Map sorting tool class
Map sorting tool class
2022-06-28 12:11:00 【A cup of bitter mustard】
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;
}
}
边栏推荐
- day28 严格模式、字符串 js 2021.09.22
- 零基础C语言(一)
- QML control type: tabbar
- Solutions to connection failures and errors when accessing mysql8 using the SSM project
- 自定义标题栏View
- IO stream of file and Base64
- js中的数组方法 2021.09.18
- AcWing 608. Poor (implemented in C language)
- Is it feasible to be a programmer at the age of 26?
- day32 js笔记 事件(上)2021.09.27
猜你喜欢

Simple understanding of ThreadLocal

Day37 JS note motion function 2021.10.11

SEO优化的许多好处是与流量有直接关系

Ali three sides: what is the difference between using on or where in the left join associated table and the condition

Deployment and optimization of vsftpd service

【C语言】判断三角形

Packaging and publishing application of jetpack compose desktop version

Source code analysis of ArrayList

【C语言】二叉树的实现及三种遍历

Convert black mask picture to color annotation file
随机推荐
day29 js笔记 2021.09.23
Fruit FL studio/cubase/studio one music host software comparison
案例驱动 :从入门到掌握Shell编程详细指南
PrecomputedTextCompat用法及原理
AGCO AI frontier promotion (2.16)
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
Zero basic C language (I)
Unity screenshot function
【C语言】随机数文件对其进行三种排序方法
Leetcode 48. 旋转图像(可以,已解决)
AcWing 610. Salary and bonus (implemented in C language)
Day23 JS notes 2021.09.14
零基础C语言(一)
If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
4. maximum continuity factor
Django -- MySQL database reflects the mapping data model to models
Android应用安全之JNI混淆
Graphics view framework for QT learning (to realize startup animation)
【C语言】NextDay问题
2018 joint examination of nine provinces & Merging of line segment trees