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的五种循环遍历方式,各种方式的性 ...
随机推荐
- 洛谷 4149 [IOI2011]Race——点分治
题目:https://www.luogu.org/problemnew/show/P4149 第一道点分治! 点分治大约是每次找重心,以重心为根做一遍树形dp:然后对于该根的每个孩子,递归下去.递归之 ...
- 我的日志app企划书1.0版本
因为个人的工作习惯,想要做一个app,是关于工作(生活)日志的. 目前有几个预想的功能吧. 1.按天展示自己的每日安排. 2.每到周末展示自己的周末安排. 1的需要: 是由于,每天总有那么一点两点的细 ...
- 在CentOS 7上安装Node.js的4种方法(包含npm)
Node.js和Javascript有着千丝万缕的联系,可以说Node.js让Javascript显得从未如此强大.好吧…微魔其实是个门外汉…但是这并不能阻碍微魔学习探索未知的信心~今天在国外闲逛,看 ...
- GPIO编程1:用文件IO的方式操作GPIO
概述 通过 sysfs 方式控制 GPIO,先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO 编号,使得该 GPIO 的操作接口从内核空间暴露到用户空间,GPIO 的 ...
- 【转】 Pro Android学习笔记(七一):HTTP服务(5):多线程调用HttpClient
目录(?)[-] 应用共享HttpClient对象的同步问题 创建共享HttpClient代码 创建共享对象 创建可共享的HttpClient对象 使用共享HttpClient对象的代码 基础代码 修 ...
- LInux在线安装JDK
1.查找Java相关列表: [root@localhost ~]# yum -y list java* 2.使用root用户安装安装时提醒必须使用root用户,sudo都不行. [root@local ...
- 发一个可伸缩线程池大小的python线程池。已通过测试。
发一个可伸缩线程池大小的线程池. 当任务不多时候,不开那么多线程,当任务多的时候开更多线程.当长时间没任务时候,将线程数量减小到一定数量. java的Threadpoolexcutor可以这样,py的 ...
- linux 下 安装mysql
安装之前,因为redhat 是yum自带的,但是不能使用,因为要交钱,还要订阅,所以需要卸载,重新安装163提供的yum 在另外一篇文章介绍 yum list mysql* 列出所有关于mysal的安 ...
- 各种浏览器下的user-agent
ie11Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko safariMozilla/5.0 (Macintos ...
- CentOS 7 破解mariadb密码
1.停止mariadb服务: systemctl stop mariadb 2.进入单用户模式: mysqld_safe --skip-grant-tables & 3.切换到另外一个工作组 ...