HashMap循环遍历方式
本教程将为你展示Java中HashMap的几种典型遍历方式。
参考文章:
Java中遍历HashMap的5种方式
如果你使用Java8,由于该版本JDK支持lambda表达式,可以采用第5种方式来遍历。
如果你想使用泛型,可以参考方法3。如果你使用旧版JDK不支持泛型可以参考方法4。
1、 通过ForEach循环进行遍历
mport java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class Test {public static void main(String[] args) throws IOException {Map<Integer, Integer> map = new HashMap<Integer, Integer>();map.put(1, 10);map.put(2, 20);// Iterating entries using a For Each loopfor (Map.Entry<Integer, Integer> entry : map.entrySet()) {System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());}}
}
2、 ForEach迭代键值对方式
如果你只想使用键或者值,推荐使用如下方式
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class Test {public static void main(String[] args) throws IOException {Map<Integer, Integer> map = new HashMap<Integer, Integer>();map.put(1, 10);map.put(2, 20);// 迭代键for (Integer key : map.keySet()) {System.out.println("Key = " + key);}// 迭代值for (Integer value : map.values()) {System.out.println("Value = " + value);}}
```bash
Map<String, String> map = new HashMap<String, String>();
for (String key : map.keySet()) {map.get(key);
}
这种方式比较耗时。
}
3、使用带泛型的迭代器进行遍历```bash
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;public class Test {public static void main(String[] args) throws IOException {Map<Integer, Integer> map = new HashMap<Integer, Integer>();map.put(1, 10);map.put(2, 20);Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();while (entries.hasNext()) {Map.Entry<Integer, Integer> entry = entries.next();System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());}}
}
4、使用不带泛型的迭代器进行遍历
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;public class Test {public static void main(String[] args) throws IOException {Map map = new HashMap();map.put(1, 10);map.put(2, 20);Iterator<Map.Entry> entries = map.entrySet().iterator();while (entries.hasNext()) {Map.Entry entry = (Map.Entry) entries.next();Integer key = (Integer) entry.getKey();Integer value = (Integer) entry.getValue();System.out.println("Key = " + key + ", Value = " + value);}}
}
5、通过Java8 Lambda表达式遍历
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class Test {public static void main(String[] args) throws IOException {Map<Integer, Integer> map = new HashMap<Integer, Integer>();map.put(1, 10);map.put(2, 20);map.forEach((k, v) -> System.out.println("key: " + k + " value:" + v));}
文章清单:
https://blog.csdn.net/qq_17556191/article/details/94871292
HashMap循环遍历方式相关推荐
- hashmap java 便利_java HashMap 循环遍历map的方法
java HashMap 循环遍历map的方法(转) import java.util.ArrayList; import java.util.HashMap; import java.util.It ...
- (转帖)HashMap循环遍历的方式
1. EntrySet循环遍历:JDK1.8以前的主要的遍历方式,效率最高 2.KeySet循环遍历:需要遍历两次,效率低 3. EntrySet迭代器循环遍历:可以删除元素 4. KeySet迭代器 ...
- Java专题 Map,hashMap 多种遍历方式 总结+统计字符出现次数案例
描述 Map 与Collection同级 键值对 数据按一对为单位存储 python中的字典 Map ->双列集合的顶层接口 HashMap-> 哈希表存储 没有固定顺序 LinkedHa ...
- HashMap的遍历方式大全
目录 一.迭代器KeySet实现 二.迭代器EntrySet实现 三.For Each KeySet实现 四.For Each EntrySet实现 五.使用Lambda实现 六.使用Streams ...
- hashMap的遍历方式
直接上代码 public static void main(String[] args){Map<Object, Object> hashMap = new HashMap<Obje ...
- HashMap 的 7 种遍历方式与性能分析!(强烈推荐)
来自:Java中文社群 随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了一个问题. 本文先从 HashMap 的遍历 ...
- HashMap 的 7 种遍历方式与性能分析!「修正篇」
这是我的第 57 篇原创文章 首先,给大家说声抱歉~ 事情经过是这样子的,五一节前我发布了一篇文章<HashMap 的 7 种遍历方式与性能分析!>,但是好心的网友却发现了一个问题,他说 ...
- HashMap 的 7 种遍历方式与性能分析!
随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了一个问题. 本文先从 HashMap 的遍历方法讲起,然后再从性能. ...
- HashMap 的 7 种遍历方式+性能分析!
随着 JDK 1.8 Streams API 的发布,使得 HashMap 拥有了更多的遍历的方式,但应该选择那种遍历方式?反而成了一个问题. 本文先从 HashMap 的遍历方法讲起,然后再从性能. ...
最新文章
- 12×××求职经过-之求职信
- WSL端口映射到win
- CodeForces - 1295C Obtain The String(dp预处理+贪心)
- 大剑无锋----SpringMVC原理详细解析
- mysql 子查询 as_mysql子查询
- c语言位运算+乘法,关于c语言中的位运算。。。
- 作者:李香菊(1964-),女,江西农业工程职业学院副教授
- 安装和启动mysql
- python骗局-python 无良培训忽悠骗局知多少?
- 发力多人在线游戏!PS5有望2020年E3展会亮相!
- 181123每日一句
- c语言程序有哪几类词汇,c语言是什么 必背词汇有哪些
- 十八个著名的心理学效应,生活中你一定用的到
- linux硬件测试拷机软件,“拷机”中。
- ORA-01031:insufficient privileges 解决方法
- Materials and Manufacturing Processes期刊投稿经验分享
- 60款非常酷的国外名片设计作品欣赏(上篇)
- 免费工具长尾,免费长尾词挖掘工具挖词的方法
- App运营推广,运维,新媒体运营与渠道
- Ceph的Paxos源码注释 - Phase 2