Java8 HashMap遍历方式性能探讨
作者:Hosee 时间:2022-01-31 08:48:11
原因:
keySet其实是遍历了2次,一次是转为Iterator对象,另一次是从hashMap中取出key所对应的value。而entrySet只是遍历了一次就把key和value都放到了entry中,效率更高。如果是JDK8,使用Map.foreach方法。
一. keySet和entrySet
Map<String, String> map = new HashMap<String, String>();
int num = 5000000;
String key, value;
// 存放500万条数据
for (int i = 1; i <= num; i++) {
key = "" + i;
value = "value";
map.put(key, value);
}
1.1 基本用法
keySet:
Map map=new HashMap();
Iterator it=map.keySet().iterator();
Object key;
Object value;
while(it.hasNext()){
key=it.next();
value=map.get(key);
System.out.println(key+":"+value);
}
时间:只获取key 50;获取key和value 170
entrySet:
Map map=new HashMap();
Iterator it=map.entrySet().iterator();
Object key;
Object value;
while(it.hasNext()){
Map.Entry entry = (Map.Entry)it.next();
key=entry.getKey();
value=entry.getValue();
System.out.println(key+"="+value);
}
时间:只获取key或获取value 时间差不多 110-140;获取key和value 140
源码上看:
keySet:
final class KeyIterator extends HashIterator
implements Iterator<K> {
public final K next() { return nextNode().key; }
}
entrySet:
final class EntryIterator extends HashIterator
implements Iterator<Map.Entry<K,V>> {
public final Map.Entry<K,V> next() { return nextNode(); }
}
其实这里已经很明显了,当要得到某个value时,keySet还需要从HashMap中get,entrySet相比keySet少了遍历table的过程,这也是两者性能上的主要差别。
for (String key1 : map.keySet()) {
value = map.get(key1);
}
时间:170
for (Entry<String, String> entry1 : map.entrySet()) {
key = entry1.getKey();
value = entry1.getValue();
}
时间:130
比较:keySet其实是遍历了2次,一次是转为Iterator对象,另一次是从hashMap中取出key所对应的value。而entrySet只是遍历了一次就把key和value都放到了entry中,效率更高。如果是JDK8,使用Map.foreach方法。
二. Map.foreach
在JDK8以后,引入了Map.foreach。
Map.foreach本质仍然是entrySet
default void forEach(BiConsumer<? super K, ? super V> action) {
Objects.requireNonNull(action);
for (Map.Entry<K, V> entry : entrySet()) {
K k;
V v;
try {
k = entry.getKey();
v = entry.getValue();
} catch(IllegalStateException ise) {
// this usually means the entry is no longer in the map.
throw new ConcurrentModificationException(ise);
}
action.accept(k, v);
}
}
配合lambda表达式一起使用,操作起来更加方便。
2.1 使用Java8的foreach+lambda表达式遍历Map
Map<String, Integer> items = new HashMap<>();
items.put("A", 10);
items.put("B", 20);
items.put("C", 30);
items.put("D", 40);
items.put("E", 50);
items.put("F", 60);
items.forEach((k,v)->System.out.println("Item : " + k + " Count : " + v));
items.forEach((k,v)->{
System.out.println("Item : " + k + " Count : " + v);
if("E".equals(k)){
System.out.println("Hello E");
}
});
时间:158
来源:https://my.oschina.net/hosee/blog/1488876
标签:Java8,HashMap,遍历,性能
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现导入导出Excel文件的方法(poi,jxl)
2021-09-13 21:12:35
Android应用创建多个快捷方式
2022-10-08 11:26:00
Java算法之递归算法计算阶乘
2021-06-30 14:10:56
![](https://img.aspxhome.com/file/2023/4/63454_0s.png)
C#将Sql数据保存到Excel文件中的方法
2023-11-11 08:34:26
Java实现批量向mysql写入数据的方法
2023-11-05 20:51:21
SpringMVC通过RESTful结构实现页面数据交互
2022-01-23 08:57:24
![](https://img.aspxhome.com/file/2023/6/80916_0s.png)
解决IDEA鼠标点击光标变大问题
2022-12-07 11:52:37
![](https://img.aspxhome.com/file/2023/4/79964_0s.jpg)
C#中使用HttpPost调用WebService的方法
2023-01-17 18:34:36
C#开发WinForm根据条件改变DataGridView行颜色
2022-05-06 05:38:56
![](https://img.aspxhome.com/file/2023/0/92880_0s.jpg)
SpringBoot数据访问自定义使用Druid数据源的方法
2023-01-10 16:29:38
![](https://img.aspxhome.com/file/2023/1/91081_0s.jpg)
Java环境配置与编译运行详解
2022-10-02 12:42:24
![](https://img.aspxhome.com/file/2023/3/88073_0s.png)
java编程中字节流转换成字符流的实现方法
2021-06-09 15:59:04
一文详解Java中Stream流的使用
2021-08-23 11:54:51
![](https://img.aspxhome.com/file/2023/0/64800_0s.png)
spring cloud 配置中心客户端启动遇到的问题
2023-03-23 16:30:34
![](https://img.aspxhome.com/file/2023/3/74883_0s.png)
Java中的AQS同步队列问题详解
2021-08-16 19:50:23
![](https://img.aspxhome.com/file/2023/3/82003_0s.jpg)
总结Java的Struts框架的异常处理方法
2022-04-12 01:29:44
![](https://img.aspxhome.com/file/2023/1/108431_0s.jpg)
Java实现月饼的制作、下单和售卖功能
2023-03-06 18:26:24
![](https://img.aspxhome.com/file/2023/4/85574_0s.png)
SpringBoot多环境开发与日志小结
2021-09-18 19:05:44
![](https://img.aspxhome.com/file/2023/9/78919_0s.png)
Springboot使用test无法启动问题的解决
2021-07-03 22:09:23
![](https://img.aspxhome.com/file/2023/2/83432_0s.png)
C#探秘系列(二)——IsXXX 系列方法
2023-06-09 01:31:16
![](https://img.aspxhome.com/file/2023/5/100095_0s.png)