当前位置:网站首页>常用的遍历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));
}
}
}
边栏推荐
- 一天学会从抓包到接口测试,通过智慧物业项目深度解析
- 环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机
- MySQL:连接查询 | 内连接,外连接
- 线程池的使用(结合Future/Callable使用)
- protobuf根据有关联的.proto文件进行编译
- 专用机终端安装软件后报IP冲突
- Modeling of the MAYA ship
- C# FileSystemWatcher
- MySQL: join query | inner join, outer join
- Falsely bamboo brother today and found a localization of API to use tools
猜你喜欢
随机推荐
不能比较或排序 text、ntext 和 image 数据类型
cmake 学习使用笔记(三)
给网站套上Cloudflare(以腾讯云为例)
MySQL:连接查询 | 内连接,外连接
U++ UE4官方文档课后作业
691. 立方体IV
busybox 知:构建
一天学会从抓包到接口测试,通过智慧物业项目深度解析
"Automatic Data Collection Based on R Language"--Chapter 3 XML and JSON
二叉树进阶复习1
TRACE32——外设寄存器查看与修改
唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
IO进程线程->进程间的通信->day7
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
Tencent Internship Summary
Redis 全套学习笔记.pdf,太全了
FPGA parsing B code----serial 4
配合屏幕录像专家,又小又清晰!
双向循环带头链表
[Shanghai] Hiring .Net Senior Software Engineer & BI Data Warehouse Engineer (Urgent)