当前位置: 首页 > news >正文

【Java基础】Map遍历的5种方式

目录

创建一个集合

方式一:Iterator 迭代器遍历

map.entrySet().iterator();

map.keySet().iterator();

方式二:For Each方式遍历

map.forEach(BiConsumer action)

方式三:获取Collection集合

map.values().forEach()

方式四:增强for遍历map

map.entrySet().for

map.keySet().for

方法五:Stream流遍历

map.entrySet().stream().forEach()

map.keySet().stream().forEach()

💟 创作不易,不妨点赞💚评论❤️收藏💙一下


创建一个集合

    HashMap<Integer, String> map = new HashMap<>();map.put(1,"ljj");map.put(2,"zsy");map.put(3,"sxf");map.put(4,"wjx");}

方式一:Iterator 迭代器遍历

map.entrySet().iterator();

map.keySet().iterator();

//Iterator 迭代器遍历public static void iterator(Map<Integer, String> map){System.out.println("---使用entrySet()迭代器进行遍历map集合---");Iterator<Map.Entry<Integer, String>> entryIterator = map.entrySet().iterator();while (entryIterator.hasNext()){Map.Entry<Integer, String> entry = entryIterator.next();System.out.println(entry.getKey()+":"+entry.getValue());}System.out.println("---使用KeySet()迭代器进行遍历map集合---");Iterator<Integer> keySetIterator = map.keySet().iterator();while (keySetIterator.hasNext()){Integer key = keySetIterator.next();System.out.println(key+":"+map.get(key));}}

方式二:For Each方式遍历

map.forEach(BiConsumer action)

 //For Each方式遍历public static void forEach_map(Map<Integer,String> map){System.out.println("map.forEach(BiConsumer action)方法遍历map");map.forEach((key,value) -> System.out.println(key+" : "+value));}

方式三:获取Collection集合

map.values().forEach()

map中的values()方法,通过这个方法可以直接获取Map中存储所有值的Collection集合

 //values()方法public static void Collection_forEach(Map<Integer,String> map){System.out.println("获取map集合的values值集合对象,进行forEach遍历");//Map中的values()方法,通过这个方法可以直接获取Map中存储所有值的Collection集合Collection<String> values = map.values();values.forEach( v -> System.out.println(v));}

方式四:增强for遍历map

map.entrySet().for

map.keySet().for

    public static void for_plus(Map<Integer,String> map){System.out.println("增强for循环,Map.Entry<>");for (Map.Entry<Integer,String> entry : map.entrySet()){String value = entry.getValue();System.out.println(value);}System.out.println("增强for循环,map.keySet");for (Integer key : map.keySet()){String value = map.get(key);System.out.println(value);}}

方法五:Stream流遍历

map.entrySet().stream().forEach()

map.keySet().stream().forEach()

//Stream流遍历public static void stream_list(Map<Integer,String> map){System.out.println("map.entrySet().stream.forEach()遍历—Stream流遍历");map.entrySet().stream().forEach((Map.Entry<Integer,String> entry) -> {System.out.print(entry.getKey()+":");System.out.println(entry.getValue());});System.out.println("map.keySet().stream.forEach()遍历—Stream流遍历");map.keySet().stream().forEach(key -> {System.out.println(map.get(key));});}

写到最后

四季轮换,已经数不清凋零了多少, 愿我们往后能向心而行,一路招摇胜!

🐋 你的支持认可是我创作的动力

💟 创作不易,不妨点赞💚评论❤️收藏💙一下

😘 感谢大佬们的支持,欢迎各位前来不吝赐教

http://www.lryc.cn/news/29209.html

相关文章:

  • 第十四届蓝桥杯三月真题刷题训练——第 2 天
  • 自然语言处理历史最全预训练模型(部署)汇集分享
  • csdn写文章自定义表格怎么做
  • Pytorch处理数据与训练网络问题汇总(协同训练)
  • 机器学习:基于神经网络对用户评论情感分析预测
  • Vue3之组件间传值避坑指南
  • 02-问题思考维度:抓住核心用户、场景化分析、需求收集与辨别、用户故事
  • C 语言编程 — GCC Attribute 语法扩展
  • LeetCode 热题 C++ 399. 除法求值 406. 根据身高重建队列
  • 提升Mac使用性能的5大方法,CleanMyMacX 2023非常的好用哦~
  • 一步一步学会给Fritzing添加元器件-丰富你的器件库
  • STM32 10个工程篇:1.IAP远程升级(一)
  • 高通Android 13默认切换免提功能
  • MySQL入门
  • 实验一 Python编程基础
  • java多线程(十五)ThreadLocal介绍和理解
  • K8S 实用工具之三 - 图形化 UI Lens
  • HDMI协议介绍(六)--EDID
  • 【项目实战】Linux下安装Nginx教程
  • 【数据结构】链式二叉树
  • CentOS安装RStudio-Server的方法
  • 从交通信号灯看流控和拥塞控制
  • 【LinkedList】| 深度剥析Java SE 源码合集Ⅰ
  • 黑马程序员7
  • Qt安装与使用经验分享;无.pro文件;无QTextCodec file;Qt小试;界面居中;无缝;更换Qt图标;更换Qt标题。
  • AAAI顶会行人重识别算法详解——Relation Network for Person Re-identification
  • hadoop调优(二)
  • 【基础算法】双指针---数组元素的目标和
  • Javascript借用原型对象继承父类型方法
  • 你不会工作1年了连枚举都还不知道吧?