HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的,下面请看实例:

package com.HashMap.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry; public class HashMapTest { public static void main(String[] args) {
HashMap<Integer, Integer> hm = new HashMap<>();
for(int i=0;i<10000000;i++){
hm.put(i, i);
} //方式1:
long currentTimeMillisStart = System.currentTimeMillis();
Iterator<Entry<Integer, Integer>> iterator = hm.entrySet().iterator();
while(iterator.hasNext()){
Entry<Integer, Integer> next = iterator.next();
//System.out.println("key: "+next.getKey()+" value: "+next.getValue());
}
long currentTimeMillisEnd =System.currentTimeMillis();
System.out.println("Time: " + (currentTimeMillisEnd-currentTimeMillisStart)); //方式2
long currentTimeMillisStart2 = System.currentTimeMillis();
Iterator<Integer> iterator2 = hm.keySet().iterator();
while(iterator2.hasNext()){
Integer next = iterator2.next();
//System.out.println("key: "+ next+" value: "+hm.get(next));
}
long currentTimeMillisEnd2 =System.currentTimeMillis();
System.out.println("Time: " + (currentTimeMillisEnd2-currentTimeMillisStart2));
} }
对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中。 
Time: 58
Time: 78

HashMap遍历方式探究的更多相关文章

  1. 史上最全HashMap遍历方式

    java Hashmap Map TreeMap 的几种遍历方式,全网最全,全网最强 package Collec2; import java.util.HashMap; import java.ut ...

  2. hashMap遍历方式

    package Ch17; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java ...

  3. Java HashMap 遍历方式探讨

    JDK8之前,可以使用keySet或者entrySet来遍历HashMap,JDK8中引入了map.foreach来进行遍历. keySet其实是遍历了2次,一次是转为Iterator对象,另一次是从 ...

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

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

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

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

  6. Java HashMap 四种遍历方式

    HashMap遍历方式包含以下4种: 1.遍历KeySet,再通过Key来getValue. 2.使用entrySet的迭代器. 3.foreach entrySet的方式. 3.foreache v ...

  7. Java中HashMap遍历的两种方式

    Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...

  8. [Java] HashMap遍历的两种方式

    Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml第一种: Map map = new HashMap( ...

  9. HashMap的两种遍历方式

    HashMap的两种遍历方式 HashMap存储的是键值对:key-value . java将HashMap的键值对作为一个整体对象(java.util.Map.Entry)进行处理,这优化了Hash ...

随机推荐

  1. php实验一

    实验准备 实验一:PHP开发环境配置,学习安装和使用集成PHP开发环境. 一. 分别下载和安装WAMP ,AppServ,XAMPP,phpstudy等软件,并测试页面. 开发环境配置 1)单独需要三 ...

  2. MySQL 主键范围查找问题

    背景: 今天遇到一个主键范围查找的情况: id是主键,每次取10000.上面的这个查询id范围越往后面消耗的时间越久.通过id自增主键去查找数据应该不会出现这个现象的.以前都没有注意这个奇怪的现象,现 ...

  3. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  4. 用SSH登录局域网中使用网络设置为NAT的虚拟机中的linux

    环境描述: A客户机,安装有putty软件使用SSH方式登录B主机中的虚拟机中的linux:A机的网络地址:192.168.1.2 B主机为Windows7平台,B主机的网络地址为:192.168.1 ...

  5. Does the OpenSceneGraph have a native file format?

    From OpenSceneGraph-3.0 onwards we have new native file formats based on generic serializers that ar ...

  6. 3ds max删除了对象后,还是将原来所有对象输出的原因

    原因是场景中除了 几何体 外还有 图形,如下图 将这些图形删除,几何体就都正常输出了.

  7. 解决spring+shiro cacheManager 登录报错

    一.项目启动,登录报错 org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...

  8. jsp 过滤器 Filter 配置

    .如果要映射过滤应用程序中所有资源: <filter>    <filter-name>loggerfilter</filter-name>    <filt ...

  9. spring刚开始学习搭建

    下载的软件包地址: http://repo.springsource.org/libs-release-local/org/springframework/spring 用maven进行快速开始: h ...

  10. JavaScript基础——处理字符串

    String对象是迄今为止在JavaScript中最常用的对象.在你定义一个字符串数据类型的变量的任何时候,JavaScript就自定为你创建一个String对象.例如: var myStr = &q ...