使用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遍历时的性能对比的更多相关文章

  1. HashMap循环遍历方式及其性能对比(zhuan)

    http://www.trinea.cn/android/hashmap-loop-performance/ ********************************************* ...

  2. HashMap循环遍历方式及其性能对比

    主要介绍HashMap的四种循环遍历方式,各种方式的性能测试对比,根据HashMap的源码实现分析性能结果,总结结论.   1. Map的四种遍历方式 下面只是简单介绍各种遍历示例(以HashMap为 ...

  3. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  4. ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转载)

    原文地址: http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 原文地址: http://www.trinea.cn ...

  5. ArrayList和LinkedList遍历方式及性能对比分析

    ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayLis ...

  6. 【转】ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    原文网址:http://www.trinea.cn/android/arraylist-linkedlist-loop-performance/ 主要介绍ArrayList和LinkedList这两种 ...

  7. Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 [ 转载 ]

    Java 集合 ArrayList和LinkedList的几种循环遍历方式及性能对比分析 @author Trinea 原文链接:http://www.trinea.cn/android/arrayl ...

  8. (转)ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...

  9. ArrayList和LinkedList的几种循环遍历方式及性能对比分析

    最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...

随机推荐

  1. DNS Doctoring

    NAT的应用可以让路由器在不同地址域内路由数据包.一个暴露在外的应用服务器,通常同时拥有了内网和外网的IP地址.这在DNS解析时可能带来麻烦. 根据DNS服务器的部署位置和配置,对同一内网中的应用服务 ...

  2. numpy.zeros(shape, dtype=float, order='C')

    numpy.zeros Return a new array of given shape and type, filled with zeros. Parameters: shape : int o ...

  3. CSS如何计算优先级?如何计算权重?

    (1) 优先级就近原则,同权重以最近者为准 载入样式以最后载入的样式为准: 同权重下:内联样式表(标签内部) > 嵌入样式表(当前文件) > 外部样式表(外部文件) !import > ...

  4. CentOS 7 安装 Zabbix 3.0

    CentOS7搭建Zabbix 一.安装数据库: 1.  安装数据库:sudo  yum  grouinstall mariadb –y 2.  启动数据库:sudo systemctl  start ...

  5. 在重命名SqlServer数据库时,报5030错误的解决办法

    数据库不能重名名5030的错误,其实很简单原因就是有应用程序正在占用这个连接,使用这样一行命令就可以查询出正在占用的连接 use master select spid from master.dbo. ...

  6. Learning Python 009 dict(字典)和 set

    Python dict(字典)和 set dict (字典)是什么东西 dict全称dictionary.为什么这个数据结构取名叫dict.因为dict的实现原理和查字典是一样的.dict使用了键-值 ...

  7. error C2512: “HelloWorld”: 没有合适的默认构造函数可用

    error C2512: "HelloWorld": 没有合适的默认构造函数可用 c++ newbie error C2512: no appropriate default co ...

  8. Ubuntu12.04安装 vsftpd

    Ubuntu12.04 FTP 的配置   ubuntu安装ftp服务器 1: 安装vsftpd ~$ sudo apt-get install vsftpd 2: 配置vsftpd 2.1 修改vs ...

  9. 1. xss原理分析与剖析(1)

    0×01 前言: <xss攻击手法>一开始在互联网上资料并不多(都是现成的代码,没有从基础的开始),直到刺的<白帽子讲WEB安全>和cn4rry的<XSS跨站脚本攻击剖析 ...

  10. 关于mysql查询最近一条记录

    关于mysql查询最近一条记录 最近项目中遇到需要查询记录当前时间最近的一条记录的问题,开始感觉无从下手,后来逐步发现了三种解决方案. 下策——查询出结果后将时间排序后取第一条 select * fr ...