当前位置:网站首页>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 Quality Officer-Civil Construction Direction-General Basic (Quality Officer) Exam Mock 100 Questions and Online Mock Exam
- [Untitled] 2022 Amination Process Exam Questions Mock Exam Question Bank and Online Mock Exam
- Switch and Router Technology-33-Static NAT
- MyEclipse数据库工具使用教程:使用驱动程序
- FPGA engineer interview questions collection 121~130
- 2021 Network Planning Designer Afternoon Case Questions
- ESP8266 教程3 — 通过TCP组建局域网并通信
- Redis中RDB和AOF的区别
- 3 Module 2: Use of scientific research tools
- Trilium使用总结
猜你喜欢

Internet Protocol 1

Switch and Router Technology - 28 - NSSA Areas for OSPF

How IP-Guard prohibits running U disk programs

【嵌入式开源库】使用J-Link打印日志,让你节省一个打印串口

C statement: data storage

ARM Architecture 4: Embedded Hardware Platform Interface Development

The use of async (asynchronous) and await

元宇宙社交应用,靠什么吸引用户「为爱发电」?

绿盾加密如何顺利切换成IP-Guard加密

C语言题解:谁是凶手!
随机推荐
[QNX Hypervisor 2.2用户手册]10.16 vdev virtio-blk
Delphi7 learning record - demo example
Prometheus :(一)基本概念
Switch and Router Technology - 25 - OSPF Multi-Area Configuration
[No 2022 Shanghai Security Officer A Certificate Exam Question Bank and Mock Exam
MySQL must know and must know (primary articles)
提升你工作效率的技巧,你得知道——Navitcat 快捷键
Delphi7学习记录-demo实例
【无2022上海市安全员A证考试题库及模拟考试
【无标题】2022年胺基化工艺考试题模拟考试题库及在线模拟考试
MFC Interprocess Communication (Shared Memory)
一些常见mysql入门练习
BitLocker的解密
【无2022上海市安全员A证考试题库及模拟考试
C语句:数据存储
宝塔Linux环境下redis开启多端口
Switch and Router Technology-33-Static NAT
shell 脚本编程---入门
Switches and routers technology - 24 - configure OSPF single area
2021 Network Planning Designer Afternoon Case Questions