当前位置:网站首页>常用的遍历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));
}
}
}
边栏推荐
- HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
- 在anaconda Promat界面import torch通过,在jupyter notebook中报错的问题(仅提供思路理解!)
- 唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
- 向美国人学习“如何快乐”
- openSource 知:社区贡献
- RK3568 environment installation
- protobuf根据有关联的.proto文件进行编译
- Modeling of the MAYA ship
- Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
- 行业应用软件项目经理三步曲
猜你喜欢
随机推荐
性能提升400倍丨外汇掉期估值计算优化案例
访问被拒绝:“microsoft.web.ui.webcontrols”的解决办法
标准C语言15
Illegal key size 报错问题
1, Citrix XenDesktop 2203 AD domain system installation (1)
binary search tree problem
外企Office常用英语
TRACE32——C源码关联1
Flink Learning 10: Use idea to write WordCount and package and run
After the firewall iptable rule is enabled, the system network becomes slow
RNote108---Display the running progress of the R program
Mysql为什么 建立数据库失败
How to avoid online memory leaks
风控特征的优化分箱,看看这样教科书的操作
工作3年,回想刚入门和现在的今昔对比,笑谈一下自己的测试生涯
TCP的粘包拆包问题+解决方案
"Automatic Data Collection Based on R Language"--Chapter 3 XML and JSON
UDP group (multi)cast
2006年星座运势全解-巨蟹
PCI Pharma Services Announces Multi-Million Dollar Expansion of UK Manufacturing Facility to Meet Growing Demand for Global High Potency Drug Manufacturing Services to Support Oncology Treatment









