当前位置:网站首页>Map根据key值排序
Map根据key值排序
2022-08-11 05:12:00 【导演1995】
import java.util.*;
/**
* @author soul
* @version version
* @date 2019/12/13 17:12
*/
public class MapSortUtil {
/**
* 对 map根据 key值排序
*
* @param map
*/
public static void mapSortByKey(Map map) {
//传进来一个key的比较器对象来构造treemap
Map<String, String> stu = new TreeMap<>(new MyComparator());
stu.putAll(map);
Set<String> keySet = stu.keySet();
Iterator it = keySet.iterator();
while (it.hasNext()) {
String next = (String) it.next();
System.out.println(next);
}
}
public static void main(String[] args) {
Map<String, String> stu = new HashMap<>();
stu.put("account", "account");
stu.put("bat", "bat");
stu.put("IBM","bat")
mapSortByKey(stu);
}
}
/**
* 内部类
*/
class MyComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
}
https://blog.csdn.net/top_code/article/details/8528954
java8写法:
List<Map.Entry<Integer,Integer>>list = map.entrySet().stream()
.sorted((entry1, entry2) -> entry1.getValue().compareTo(entry2.getValue()))
.collect(Collectors.toList());
边栏推荐
- 2022 building welder (building a special type of work) examination questions and simulation test
- Mysql introductory exercise
- MySQL数据库管理
- HAVE FUN | "SOFA Planet" spacecraft plan, the latest progress of source code analysis activities
- ESP8266 教程3 — 通过TCP组建局域网并通信
- 用白嫖的Adobe正版软件,减少应届毕业生的慢就业、不就业等现象
- Trilium使用总结
- Four functional interfaces
- async(异步)和await的使用
- Use Adobe genuine software for prostitution to reduce the slow employment and non-employment of fresh graduates
猜你喜欢

四大函数式接口

Switches and routers technology - 26 - configure OSPF peripheral area

Switch and Router Technology-34-Dynamic NAT

Unity WebGL RuntimeError: integer overflow

C语句:数据存储

3 Module 2: Use of scientific research tools

什么是三次握手和四次挥手(清晰易懂)
![[ARM] rk3399 mounts nfs error](/img/0c/f9f0a2f3850cd55d8bebbee7f898de.png)
[ARM] rk3399 mounts nfs error

普林斯顿微积分读本05第四章--求解多项式的极限问题

Sub-database sub-table ShardingSphere-JDBC notes arrangement
随机推荐
分库分表之sharding-proxy
Some common mysql entry exercises
【无2022上海市安全员A证考试题库及模拟考试
Tips to make your code more and more taller and taller - code specification, you have to know
3 Module 2: Use of scientific research tools
IP-Guard如何禁止运行U盘程序
oracle tablespace and user creation
ARM结构体系4:嵌入式硬件平台接口开发
redis分布式锁
Switch and Router Technology - 22/23 - OSPF Dynamic Routing Protocol/Link State Synchronization Process
[Embedded open source library] The use of cJSON, an efficient and streamlined json parsing library
网络协议1
Switch and Router Technology-35-NAT to PAT
How to switch Green Shield encryption to IP-Guard encryption smoothly
金仓数据库 KingbaseGIS 使用手册(6.8. 几何对象输入函数)
Zabbix builds enterprise-level monitoring and alarm platform
[Untitled] 2022 Amination Process Exam Questions Mock Exam Question Bank and Online Mock Exam
C语句:数据存储
HAVE FUN | "SOFA Planet" spacecraft plan, the latest progress of source code analysis activities
Switch and Router Technology-29-OSPF Virtual Link