当前位置:网站首页>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;
}
}
边栏推荐
- fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
- Day29 JS notes 2021.09.23
- AsyncTask经验小结
- Allez, Meta - Cosme, comme prévu, cette chaleur ne durera pas longtemps.
- JS foundation 10
- day29 js笔记 2021.09.23
- Mutual conversion between mytipartfile and file
- AcWing 607. Average 2 (implemented in C language)
- 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
- 纯纯大怨种!那些年被劝退的考研专业
猜你喜欢

Leetcode 48. 旋转图像(可以,已解决)

开源项目维权成功案例: spug 开源运维平台成功维权

Day30 JS notes BOM and DOM 2021.09.24

Day34 JS notes regular expression 2021.09.29

Why do many people want to change careers as programmers, while some programmers want to change careers as others?

.NET混合开发解决方案24 WebView2对比CefSharp的超强优势

【C语言】关于scanf()与scanf_s()的一些问题

Packaging and publishing application of jetpack compose desktop version

Redis 原理 - List

Remote login sshd service
随机推荐
Oracle date format exception: invalid number
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
【vi/vim】基本使用及命令汇总
Contract quantitative trading system development | contract quantitative app development (ready-made cases)
day33 js笔记 事件(下)2021.09.28
6.A-B
PrecomputedTextCompat用法及原理
What method is required for word, PDF and txt files to realize full-text content retrieval?
【C语言】文件读写函数使用
Day31 JS notes DOM 2021.09.26
Int~long long indicates the maximum and minimum number
【北京航空航天大学】考研初试复试资料分享
MapReduce项目案例1
水果FL Studio/Cubase/Studio one音乐宿主软件对比
Interview skills for interview steps
MapReduce project case 3 - temperature statistics
建立自己的网站(18)
IO stream of file and Base64
Timestamp and date conversion "suggested collection"
Three ways to implement LRU cache (recommended Collection)