HashMap遍历方式
// 通过Map.values()遍历所有的value,但不能遍历key
for(String v:map.values()){System.out.println("The value is "+v);
}
// 迭代器 EntrySet 方式遍历 -- 性能稍好 一次取值
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){Map.Entry<Integer, String> next = iterator.next();System.out.println(next.getKey());System.out.println(next.getValue());
}
// 迭代器的 KeySet 方式遍历
Iterator<Integer> iterator = map.keySet().iterator();
while (iterator.hasNext()){Integer key = iterator.next();System.out.print(key);System.out.print(map.get(key));
}
// For Each EntrySet 方式遍历 -- 推荐,尤其是容量大时
for (Map.Entry<Integer,String> entry: map.entrySet()) {System.out.println("entry.getKey() = " + entry.getKey());System.out.println("entry.getValue() = " + entry.getValue());
}
// For Each KeySet 方式遍历 -- 普通使用,二次取值(性能差)
for (Integer key: map.keySet()) {System.out.println(key);System.out.println(map.get(key));
}
// Lambda 表达式方式遍历
map.forEach((key,value) -> {System.out.print(key);System.out.print(value);});
// Streams API 单线程方式遍历
map.entrySet().stream().forEach((integerStringEntry -> {System.out.println(integerStringEntry.getKey());System.out.println(integerStringEntry.getValue());}));
// Streams API 多线程方式遍历
map.entrySet().parallelStream().forEach((integerStringEntry -> {System.out.println(integerStringEntry.getKey());System.out.println(integerStringEntry.getValue());}));
HashMap遍历方式相关推荐
- Java HashMap 遍历方式性能探讨
转载自 Java HashMap 遍历方式性能探讨 关于HashMap的实现这里就不展开了,具体可以参考JDK7与JDK8中HashMap的实现 JDK8之前,可以使用keySet或者entrySet ...
- Java 基础——HashMap 遍历方式
目录 1.使用迭代器 (Iterator) EntrySet 的方式进行遍历 2.使用迭代器 (Iterator) KeySet 的方式进行遍历 3.使用 foreach EntrySet 的方式进行 ...
- Java之hashMap遍历方式
在java开发中,hashMap是非常重要的容器类,存储的是键值对(key,value). HashMap继承AbstractMap,实现了Map.Cloneable.Serializable接口,非 ...
- java遍历hashmapk v_Java HashMap 遍历方式探讨
JDK8之前,可以使用keySet或者entrySet来遍历HashMap,JDK8中引入了map.foreach来进行遍历. keySet其实是遍历了2次,一次是转为Iterator对象,另一次是从 ...
- HashMap 遍历方式
HashMap的遍历可以用entrySet():keySet()可以获得key,根据key可以用get(key)获取value :values()可以获取map里所有的值,返回的是一个Collecti ...
- 学习:HashMap遍历方式
1.使用keySet()方法: 使用keySet方法拿到map集合中所有的键,存储在set集合中,foreach遍历set集合,通过键获取值. Set<Car> cars = carStr ...
- Java中 List、Set、Map遍历方式以及性能比较
目录 一.简介 二.遍历方式 1.ArrayList遍历方式 (1)for循环遍历 (2)foreach循环遍历 (3)Iterator迭代器遍历 2.LinkedList遍历方式 (1)for循环遍 ...
- HashMap 的 7 种遍历方式与性能分析!(强烈推荐)
来自:Java中文社群 随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了一个问题. 本文先从 HashMap 的遍历 ...
- HashMap遍历的两种方式,推荐使用entrySet()
转自:HashMap遍历的两种方式,推荐使用entrySet() 第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterat ...
最新文章
- Vant简单H5 web app【小试牛刀】
- java二级选择题要对一半吗_据说一半以上的java程序员会出错的题
- AgilePoint BPMS 业务流程
- STM32 USART1 USART2 UASRT3 UART4 UART5串口通信测试程序
- mysql的my.cnf
- 海南大学计算机科学与技术专业考研,2021年海南大学计算机科学与技术(081200)硕士研究生招生信息_考研招生计划和招生人数 - 学途吧...
- rh9.0虚拟机dmesg启动过程分析(1)
- redis集群部署及常用的操作命令_01
- linux复制文件跳过相同,Linux cp指令,怎么跳过相同的文件
- [html] link标签的属性media有哪些值?都有什么作用?
- 江苏成教计算机统考操作题多少分,江苏省成人计算机统考试题,操作题.doc
- 趣说技术 | 用MQ 能完全替代 RPC 调用?
- MapReduce 示例:减少 Hadoop MapReduce 中的侧连接
- java 循环写法_java的for循环的几种写法
- erp系统实施方案会遇到哪些问题?
- html打开txt文件怎么打开是乱码了,使用记事本打开TXT文件变乱码的解决办法
- 走迷宫小游戏课设(C语言)
- 验证是不是合法的18位身份证号码代码
- 使用aria2批量下载微博相册
- unite17-shanghai-JPLee-netease-pangu-FullChinese
热门文章
- I00011 打印包含7的三位数
- 最佳实践 —— 单元测试
- 语言的学习 —— 西班牙语(español)
- 作为电磁波的 Wi-Fi 信号
- 中英文对照 —— 数学
- 使用 matlab 数字图像处理(六)—— 空域滤波
- Python 数据结构与算法——从二分图到寻找最大排列(Maximum Permutation)
- 实用的工具 —— 百度云、everything(全局搜索)、Everest(硬件检测)、TechPowerUp GPU-Z
- php 三个等号与两个等号,浅析JavaScript和PHP中三个等号(===)和两个等号(==)的区别...
- python项目开发实例-Python小项目:快速开发出一个简单的学生管理系统