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. 细看INNODB数据落盘

    本文来自:沃趣科技 http://www.woqutech.com/?p=1459 1.  概述 前面很多大侠都分享过MySQL的InnoDB存储引擎将数据刷新的各种情况.我们这篇文章从InnoDB往 ...

  2. List,Set,Map用法以及区别

    List,Set,Map是否继承自Collection接口? 答:List,Set是,Map不是. 如图: Collection ├List │├LinkedList │├ArrayList │└Ve ...

  3. 【leetcode】Remove Nth Node From End of List(easy)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  4. 【python】Python遍历dict的key最高效的方法是什么?

    来源:https://segmentfault.com/q/1010000002581747 方法一:直接遍历 速度快 for key in _dict: pass 方法二:iterkeys() 速度 ...

  5. JDBC题库

    一.    填空题 JDBC    ,是一种用于执行SQL语句的Java API,为多种关系数据库提供统一访问.它由一组用Java语言编写的类和接口组成. JDBC API:供程序员调用的接口与类,集 ...

  6. express响应前端ajax请求

    后端其实并不需要知道前端发起的请求是不是ajax,后端只需要响应请求即可.例子: 前端这样写: $('button').on('click', function(event) { event.prev ...

  7. 读取Spring的配置文件applicationContext.xml的5种方法

    1.利用ClassPathXmlApplicationContext,这种方式配置文件应该放在类包同路径下Java代码: ApplicationContext ct=new ClassPathXmlA ...

  8. 使用detours实现劫持

    第一步:下载detours3.0,安装detours 第二步:构建库文件,nmake编译 第三步:包含库文件和头文件 #include "detours.h" //载入头文件 #p ...

  9. codevs 1488GangGang的烦恼

    题目链接:http://codevs.cn/problem/1488/ 写个高精度大数运算就行 #include<cstdio> #include<iostream> #inc ...

  10. history 清空历史记录 或 history不记录历史命令

    # vi ~/.bash_history 清空里面的记录,并退出当前shell # exit(一定要退出当前shell) # history 1 vi ~/.bash_history 2 histor ...