[Java] HashMap遍历的两种方式
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/
还是第一种好,简单。。。
[Java] HashMap遍历的两种方式的更多相关文章
- Java中HashMap遍历的两种方式
Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...
- HashMap遍历的两种方式
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { ...
- HashMap 遍历的两种方式及性能比较
HashMap 是Java开发中经常使用的数据结构.相信HashMap 的基本用法你已经很熟悉了.那么我们该如何遍历HashMap 呢?哪种遍历方式的性能更好呢?本篇文章来为你解决这个疑惑. 一.Ha ...
- HashMap遍历的两种方式,推荐使用entrySet()
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { ...
- 对Java代码加密的两种方式,防止反编译
使用Virbox Protector对Java项目加密有两种方式,一种是对War包加密,一种是对Jar包加密.Virbox Protector支持这两种文件格式加密,可以加密用于解析class文件的j ...
- Java新建线程的两种方式
Java新建线程有两种方式,一种是通过继承Thread类,一种是实现Runnable接口,下面是新建线程的两种方式. 我们假设有个竞赛,有一个选手A做俯卧撑,一个选手B做仰卧起坐.分别为两个线程: p ...
- python中字典的循环遍历的两种方式
开发中经常会用到对于字典.列表等数据的循环遍历,但是python中对于字典的遍历对于很多初学者来讲非常陌生,今天就来讲一下python中字典的循环遍历的两种方式. 注意: python2和python ...
- C++ 数组遍历的两种方式
C++ 数组遍历的两种方式: #include <iostream> using namespace std; int main() { // 一维数组 ] = {, , , , }; / ...
- java集合遍历的几种方式总结及比较
集合类的通用遍历方式, 用迭代器迭代: Iterator it = list.iterator(); while(it.hasNext()) { Object obj = it.next(); } ...
随机推荐
- Windows 消息机制详解
总的来说: MSG包括: 窗口句柄,指示MSG发送的目的窗口 消息标识 lPARAM.wParam 发送时间 发送时的鼠标位置 关于消息队列: Windows系统有一个系统消息队列 每个线程都有一 ...
- HeapAlloc、GlobalAlloc和new等内存分配有什么区别么?
查找了一些 new , GlobalAlloc, HeapAlloc分配内存方式的区别. 转了一些资料 //============================================== ...
- SQL Script 杂记
1.提交sql server中未提交的事务 commit select @@TRANCOUNT 2.查询存储过程中包含某个字符串的所有存储过程 SELECT *FROM INFORMATION ...
- linux自启动服务方式
方式一: /etc/init.d/servicename restart 编写 /etc/init.d/ 下面的标准的服务shell脚本 方式二: vim /etc/rc.local /home/ ...
- libevent中定时器的使用方法
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <signal.h ...
- NAND flash NOR flash SDRAM区别
nand flash:适合大容量数据存储,类似硬盘:nor flash:适合小容量的程序或数据存储,类似小硬盘:sdram:主要用于程序执行时的程序存储.执行或计算,类似内存. 区别:nor flas ...
- [p2p]UDP用打洞技术穿透NAT的原理与实现
首先先介绍一些基本概念: NAT(Network Address Translators),网络地址转换:网络地址转换是在IP地址日益缺乏的情况下产生的, ...
- CloudStack全局参数
{ "listconfigurationsresponse": { "count": 305, "config ...
- jqGrid初次使用遇到的问题及解决方法
问题一:初始化定义翻页用的导航栏时,表中出现"undefined"方框: 解决:需要导入grid.locale-cn.js文件. 问题二:页面只有一页,无法翻页: 解决:初始化设置 ...
- winform 在指定目录下已经生成资源Image图片的方式
假设在项目目录下存在一个Image目录,注意其中图片已经都设置成为:生成方式为资源文件. /// <summary> /// 得到要绘置的图片对像 /// </summary> ...