HashMap遍历时的性能对比
使用KeySet和EntrySet遍历的差别
public static void main(String[] args) {
HashMap<Integer, Integer> hasMap = new HashMap();
for (int i = ; i < ; i++) {
hasMap.put(i, i);
}
Long j = System.currentTimeMillis();
for (int i = ; i < ; i++) {
for (Map.Entry<Integer, Integer> integerIntegerEntry : hasMap.entrySet()) {
integerIntegerEntry.getKey();
integerIntegerEntry.getValue();
}
}
System.out.println("使用EntrySet:" + (System.currentTimeMillis() - j));
Long k = System.currentTimeMillis();
for (int i = ; i < ; i++) {
for (Integer key : hasMap.keySet()) {
hasMap.get(key);
}
}
System.out.println("使用KeySet:" + (System.currentTimeMillis() - k));
}
运行多次后,两者差别有2秒左右
结论:使用EntrySet遍历时性能更高。《阿里开发手册》中也推荐使用EntrySet
原因分析:。。。
JDK8中Map interface有个default实现方法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);
}
}
HashMap遍历时的性能对比的更多相关文章
- HashMap循环遍历方式及其性能对比(zhuan)
http://www.trinea.cn/android/hashmap-loop-performance/ ********************************************* ...
- HashMap循环遍历方式及其性能对比
主要介绍HashMap的四种循环遍历方式,各种方式的性能测试对比,根据HashMap的源码实现分析性能结果,总结结论. 1. Map的四种遍历方式 下面只是简单介绍各种遍历示例(以HashMap为 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转载)
原文地址: http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 原文地址: http://www.trinea.cn ...
- ArrayList和LinkedList遍历方式及性能对比分析
ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayLis ...
- 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析
原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...
- Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]
Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...
- (转)ArrayList和LinkedList的几种循环遍历方式及性能对比分析
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...
随机推荐
- Shiro 权限管理filterChainDefinitions过滤器配置
博客转载:http://blog.csdn.net/userrefister/article/details/47807075 /** * Shiro-1.2.2内置的FilterChain * @s ...
- 时空上下文视觉跟踪(STC)算法
论文原文以及Matlab代码下载 算法概述 而STC跟踪算法基于贝叶斯框架,根据跟踪目标与周围区域形成的的时空关系,在图像低阶特征上(如图像灰度和位置)对目标与附近区域进行了统计关系建模.通过计算置信 ...
- 在Altium Designer 2009下如何添加Logo图
最近用Altium Designer 2013(14.2)绘制PCB,之后想在板子上放置一个LOGO图.要是用Altium Designer10以前的版本,过程也很简单,可在用Altium Desig ...
- 想要table表格垂直滚动,加点CSS即可
<style> /*设置 tbody高度大于400px时 出现滚动条*/ table tbody { display: block; height: 400px; overflow-y: ...
- bootstrap插件使用
首先,针对导航栏(单页面导航插件(缓慢跳转)) 引入插件:jquery.singlePageNav.min.js 下载链接:http://pan.baidu.com/s/1jHBaQxg 密码: ...
- MQTT,XMPP,STOMP,AMQP,WAMP适用范围优缺点比较
想要向服务器发送请求并获得响应?直接使用 HTTP 吧!非常简单.但是当需要通过持久的双向连接来通信时,如 WebSockets,当然你也有其它的选择. 这篇文章会简单扼要的解释 MQTT,XMPP, ...
- AngularJS(Part 10)--页面导航
页面导航 过去,一个URL代表一个页面.但是随着Ajax的兴起,情况发生的很大的变化.不同的内容可以使用同一个URL.这让浏览器中的回退.前进甚至收藏按钮都失去了作用.而AngularJS提供 ...
- map-reduce的八个流程
下面讲解这八个流程 Inputformat-->map-->(combine)-->partition-->copy&merge-->sort-->red ...
- 由count(sno)和count(cno)引发的思考
最近在练习sql语句,在一个select查询语句上有理解性偏差,现整理汇总下相关知识点. 首先,说下这个问题吧. 问题是:查询选课人数大于等于2人的课程编号以及选课的人数 具体的表结构信息: 我自己的 ...
- hdu1051
#include<iostream> #include<algorithm> using namespace std; struct SIZE { int l; int w; ...