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. 几种Linux 查询外网出口IP的方法

    Curl 纯文本格式输出: curl icanhazip.com curl ifconfig.me curl curlmyip.com curl ip.appspot.com curl ipinfo. ...

  2. tp5文件上传

    //tp5上传文件先 use think\File; //上传文件处理 $file = request()->file('file'); // 获取表单提交过来的文件 $error = $_FI ...

  3. 安装Django,运行django-admin.py startproject 工程名,后不出现指定的工程解决办法!!

       第一次写博客,,,,, 在看我这篇教程的前提是你应该已经正确装好python和Django了,好了,废话不说了,正题走你!你现在是不是很纠结自己运行django-admin.py startpr ...

  4. osg 纹理访问器

    #include<osgViewer/Viewer> #include<osg/Node>#include<osg/Geode>#include<osg/Gr ...

  5. 双栈排序(codevs 1170)

    题目描述 Description Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈 ...

  6. 对于类的双重调用的demo

    代码如下: package cao.com.duixiang; public class TestCCircle { public static void main(String[] args) { ...

  7. 一个TextView内显示不同颜色的文字

    String format = "<font color='#FC8262'>%s</font>:%s"; String text = String.for ...

  8. 浅析 - 提高xib(Interface Builder)高效工作的几个小技巧

    本文译自:8 Tips for working effectively with Interface Builder(需FQ)先来看看目录:介绍使view的Size与view中的Content相适应按 ...

  9. NYOJ题目112指数运算

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs0AAAIICAIAAAAaCETRAAAgAElEQVR4nO3drW7jWtwv4PcmwnMhxb ...

  10. php 上传文件实例 上传并下载word文件

    上传界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...