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

第一种:
  Map map = new HashMap();
  Iterator iter = map.entrySet().iterator();
  while (iter.hasNext()) {
  Map.Entry entry = (Map.Entry) iter.next();
  Object key = entry.getKey();
  Object val = entry.getValue();
  }
  效率高,以后一定要使用此种方式!
第二种:
  Map map = new HashMap();
  Iterator iter = map.keySet().iterator();
  while (iter.hasNext()) {
  Object key = iter.next();
  Object val = map.get(key);
  }
  效率低,以后尽量少使用!
 
       HashMap的遍历有两种常用的方法,那就是使用keyset及entryset来进行遍历,但两者的遍历速度是有差别的,下面请看实例:
  public class HashMapTest {
  public static void main(String[] args) ...{
  HashMap hashmap = new HashMap();
  for (int i = 0; i < 1000; i ) ...{
  hashmap.put("" i, "thanks");
  }
  long bs = Calendar.getInstance().getTimeInMillis();
  Iterator iterator = hashmap.keySet().iterator();
  while (iterator.hasNext()) ...{
  System.out.print(hashmap.get(iterator.next()));
  }
  System.out.println();
  System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
  listHashMap();
  }
  public static void listHashMap() ...{
  java.util.HashMap hashmap = new java.util.HashMap();
  for (int i = 0; i < 1000; i ) ...{
  hashmap.put("" i, "thanks");
  }
  long bs = Calendar.getInstance().getTimeInMillis();
  java.util.Iterator it = hashmap.entrySet().iterator();
  while (it.hasNext()) ...{
  java.util.Map.Entry entry = (java.util.Map.Entry) it.next();
  // entry.getKey() 返回与此项对应的键
  // entry.getValue() 返回与此项对应的值
  System.out.print(entry.getValue());
  }
  System.out.println();
  System.out.println(Calendar.getInstance().getTimeInMillis() - bs);
  }
  }
  对于keySet其实是遍历了2次,一次是转为iterator,一次就从hashmap中取出key所对于的value。而entryset只是遍历了第一次,他把key和value都放到了entry中,所以就快了。

Java中HashMap遍历的两种方式(本教程仅供研究和学习,不代表JAVA中文网观点)
本篇文章链接地址:http://www.javaweb.cc/language/java/032291.shtml
如需转载请注明出自JAVA中文网:http://www.javaweb.cc/

还是第一种好,简单。。。

hashMap_使用的更多相关文章

  1. [Project] SpellCorrect源码详解

    该Project原来的应用场景是对电商网站中输入一个错误的商品名称进行智能纠错,比如iphoae纠错为iphone.以下介绍的这个版本对其作了简化,项目源代码地址参见我的github:https:// ...

  2. LRU算法 - LRU Cache

    这个是比较经典的LRU(Least recently used,最近最少使用)算法,算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. 一般应 ...

  3. [转]如何用C++实现一个LRU Cache

    [转自http://hawstein.com/posts/lru-cache-impl.html] LRU是Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法 ...

随机推荐

  1. openstack controller ha测试环境搭建记录(十一)——配置neutron(网络节点)

    在网络节点配置内核参数:vi /etc/sysctl.confnet.ipv4.ip_forward=1net.ipv4.conf.all.rp_filter=0net.ipv4.conf.defau ...

  2. Centos rsync文件同步配置

    一.服务器端配置: # yum -y install xinetd   CentOS默认已经安装了rsync 服务.. 输入 rsync 命令可查看是否安装.   # vi /etc/xinetd.d ...

  3. (中等) HDU 1542 Atlantis,扫描线。

    Problem Description There are several ancient Greek texts that contain descriptions of the fabled is ...

  4. with补充知识点

    import threading,queue,time import contextlib @contextlib.contextmanager def fun(list_1,val): list_1 ...

  5. ios UIKit动力

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...

  6. Android与JNI(三) ---- c++调用java(转载)

    源码下载:JniDemo JNI就是Java Native Interface, 即可以实现Java调用本地库, 也可以实现C/C++调用Java代码, 从而实现了两种语言的互通, 可以让我们更加灵活 ...

  7. u-boot源码下载

    1. 使用git下载u-boot源码  1.1 clone u-boot源码仓库 可以使用git.HTTP.和 rsync协议来下载u-boot源码.你可以使用如下方法来克隆源码树: $ git cl ...

  8. CentOS 7 上面安装PowerShell

    看了文章 爱上PowerShell , 就想在CentOS 7上面试试PowerShell , 本文记录了在CentOS 7.2上安装Powershell 的过程. 首先我们要从github上下载最新 ...

  9. iOS 之 系统机制

    iOS 沙盒 iOS 8 之 新特性 iOS 操作系统整体架构层次讲解

  10. 如何使用Grunt(好文)

    Grunt 是什么? Grunt 基于Node.js之上,是一个以任务处理为基础的命令行工具,可以减少优化开发版本为发布版本所需的人力和时间,从而加速开发流程.它的工作原理是把这 些工作整合为不同的任 ...