当前位置:网站首页>常用的遍历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));
}
}
}
边栏推荐
- 二叉树进阶复习1
- Flink Learning 10: Use idea to write WordCount and package and run
- MySQL: order by sorting query, group by grouping query
- Unity—物理引擎+“武器模块”
- 奇怪的Access错误
- 400 times performance improvement 丨 swap valuation optimization case calculation
- 七夕?编程?
- After the firewall iptable rule is enabled, the system network becomes slow
- 2022起重机司机(限桥式起重机)考试题库及模拟考试
- 2022 crane driver (limited bridge crane) exam question bank and simulation test
猜你喜欢

U++ UE4官方文档课后作业

MySQL: order by sorting query, group by grouping query

Summary of Text Characterization Methods

TCP的粘包拆包问题+解决方案

Put Cloudflare on the website (take Tencent Cloud as an example)

性能提升400倍丨外汇掉期估值计算优化案例

软件测试必问面试题(附答案和解析)

Vulnhub target drone: HA_ NARAK

Flink学习11:flink程序并行度

400 times performance improvement 丨 swap valuation optimization case calculation
随机推荐
693. 行程排序
protobuf根据有关联的.proto文件进行编译
RNote108---显示R程序的运行进度
Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
数据库——概述
Illegal key size 报错问题
真实字节跳动测试开发面试题,拿下年薪50万offer。
[instancetype type Objective-C]
Flink Learning 11: Flink Program Parallelism
Redis常用命令
Re regular expressions
国家强制性灯具安全标准GB7000.1-2015
HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
在anaconda Promat界面import torch通过,在jupyter notebook中报错的问题(仅提供思路理解!)
moment的使用
browserslist 选项的目的是什么?
Mysql master-slave delay reasons and solutions
环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机
【 LeetCode 】 235. A binary search tree in recent common ancestor
C# FileSystemWatcher