当前位置:网站首页>常用的遍历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));
}
}
}
边栏推荐
猜你喜欢
随机推荐
在anaconda Promat界面import torch通过,在jupyter notebook中报错的问题(仅提供思路理解!)
An IP conflict is reported after installing the software on a dedicated computer terminal
TRACE32——List源代码查看
Shiny04---Application of DT and progress bar in shiny
Hash these knowledge you should also know
唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
Summary of Text Characterization Methods
It turns out that Maya Arnold can also render high-quality works!Awesome Tips
Mysql 死锁和死锁的解决方案
Discourse 清理存储空间的方法
mysql使用in函数的一个小问题
691. 立方体IV
Why does Mysql fail to create a database
一天学会从抓包到接口测试,通过智慧物业项目深度解析
真实字节跳动测试开发面试题,拿下年薪50万offer。
2022 crane driver (limited bridge crane) exam question bank and simulation test
GAN generates anime avatar Pytorch
Shiny04---DT和进度条在shiny中的应用
RK3568环境安装