当前位置:网站首页>常用的遍历map的方法
常用的遍历map的方法
2022-08-05 07:13:00 【MrLee528】
常用的遍历map的方法
package com.lxh.config.utils;
import java.util.*;
/** * @ClassName: commonUtil * @Author: lxh * @Description: 公共方法工具类 * @Date: 2022/4/14 14:53 */
public class CommonMethods {
public static void main(String[] args) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("id", 1);
map.put("name", "lxh");
map.put("age", 18);
getMap(1, map);
getMap(2, map);
getMap(3, map);
getMap(4, map);
getMap(5, map);
}
/** * 遍历map */
public static void getMap(Integer type, Map<String, Object> map) {
if (type == 1) {
System.out.println("============通过map.keySet()的key获取value===========");
Set<String> set = map.keySet();
set.forEach(item -> {
System.out.println(item + ": " + map.get(item));
});
} else if (type == 2) {
System.out.println("============通过map.entrySet()的实体去获取key跟value===========");
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
entrySet.forEach(item -> {
System.out.println(item.getKey() + ": " + item.getValue());
});
} else if (type == 3) {
System.out.println("=============通过迭代器获取key跟value==========");
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
System.out.println(entry.getKey() + ": " + entry.getValue());
}
} else if (type == 4) {
System.out.println("============通过map.values()获取value,不能获取key===========");
map.values().forEach(System.out::println);
} else if (type == 5) {
System.out.println("============通过lambda表达式获取key跟value===========");
map.forEach((key, value) -> System.out.println(key + ": " + value));
}
}
}
边栏推荐
- C# FileSystemWatcher
- Bluetooth gap protocol
- Shiny04---Application of DT and progress bar in shiny
- 标准C语言15
- MobileNetV1架构解析
- TRACE32——加载符号表信息用于调试
- An IP conflict is reported after installing the software on a dedicated computer terminal
- 2022.7.29好题选讲(计数专题)
- Technical Analysis Mode (8) Double Top and Bottom
- binary search tree problem
猜你喜欢
It turns out that Maya Arnold can also render high-quality works!Awesome Tips
字节面试流程及面试题无私奉献,吐血整理
Tencent Internship Summary
HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
Put Cloudflare on the website (take Tencent Cloud as an example)
Shiny02---Shiny exception solution
Shiny02---Shiny异常解决
Flink Learning 11: Flink Program Parallelism
Mysql为什么 建立数据库失败
MySQL:连接查询 | 内连接,外连接
随机推荐
After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career
《基于R语言的自动数据收集》--第3章 XML和JSON
[Tool Configuration] Summary of Common Uses of VSCode
Hash these knowledge you should also know
Invalid operator for data type.The operator is add and the type is text.
字节面试流程及面试题无私奉献,吐血整理
【 LeetCode 】 235. A binary search tree in recent common ancestor
re正则表达式
Put Cloudflare on the website (take Tencent Cloud as an example)
【instancetype类型 Objective-C】
DeFi 前景展望:概览主流 DeFi 协议二季度进展
线程池的创建及参数设置详解
【工具配置篇】VSCode 常用使用总结
任务流调度工具AirFlow,,220804,,
MAYA船的建模
Hash 这些知识你也应该知道
2022起重机司机(限桥式起重机)考试题库及模拟考试
C# FileSystemWatcher
"Automatic Data Collection Based on R Language"--Chapter 3 XML and JSON
protobuf根据有关联的.proto文件进行编译