当前位置:网站首页>常用的遍历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
- A small problem with mysql using the in function
- 栈与队列的基本介绍和创建、销毁、出入、计算元素数量、查看元素等功能的c语言实现,以及栈的压入、弹出序列判断,栈结构的链式表示与实现
- 693. 行程排序
- busybox 知:构建
- Cannot compare or sort text, ntext, and image data types
- RNote108---显示R程序的运行进度
- Flink学习11:flink程序并行度
- 字符串提取 中文、英文、数字
- Invalid operator for data type.The operator is add and the type is text.
猜你喜欢

re正则表达式

Flink Learning 10: Use idea to write WordCount and package and run

Shiny04---Application of DT and progress bar in shiny

环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机

400 times performance improvement 丨 swap valuation optimization case calculation

Falsely bamboo brother today and found a localization of API to use tools

Hash 这些知识你也应该知道

Shiny04---DT和进度条在shiny中的应用

Use of thread pool (combined with Future/Callable)

线程池的创建及参数设置详解
随机推荐
【 LeetCode 】 235. A binary search tree in recent common ancestor
C-Eighty seven(背包+bitset)
IO process thread -> communication between processes -> day7
TRACE32——通用寄存器查看与修改
【LeetCode】235.二叉搜索树的最近公共祖先
Week 8 Document Clustering(文本聚类)
专用机终端安装软件后报IP冲突
算法拾遗十五补链表相关面试题
protobuf根据有关联的.proto文件进行编译
Why does Mysql fail to create a database
向美国人学习“如何快乐”
3555. 二叉树
Cannot compare or sort text, ntext, and image data types
[Tool Configuration] Summary of Common Uses of VSCode
TRACE32——外设寄存器查看与修改
标准C语言15
每一个女孩曾经都是一个没有泪的天使
UDP broadcast
MySQL:连接查询 | 内连接,外连接
奇怪的Access错误