当前位置:网站首页>常用的遍历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));
}
}
}
边栏推荐
- Re regular expressions
- 【instancetype类型 Objective-C】
- Use of thread pool (combined with Future/Callable)
- 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
- Invalid operator for data type.The operator is add and the type is text.
- 国家强制性灯具安全标准GB7000.1-2015
- Flink学习11:flink程序并行度
- Flink学习10:使用idea编写WordCount,并打包运行
- 工作3年,回想刚入门和现在的今昔对比,笑谈一下自己的测试生涯
- MySQL:连接查询 | 内连接,外连接
猜你喜欢
随机推荐
protobuf根据有关联的.proto文件进行编译
今天虚竹哥又发现了一款好用的国产化API工具
[instancetype type Objective-C]
Tencent Internship Summary
2022 crane driver (limited bridge crane) exam question bank and simulation test
1, Citrix XenDesktop 2203 AD domain system installation (1)
After the firewall iptable rule is enabled, the system network becomes slow
MySQL:order by排序查询,group by分组查询
Mysql为什么 建立数据库失败
外企Office常用英语
(4) Rotating object detection data roLabelImg to DOTA format
moment的使用
An IP conflict is reported after installing the software on a dedicated computer terminal
线性代数对角化
busybox 知:构建
It turns out that Maya Arnold can also render high-quality works!Awesome Tips
props 后面的数据流是什么?
文本特征化方法总结
TRACE32——Go.direct
mysql使用in函数的一个小问题









