[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(); } ...
随机推荐
- Leveldb Advanced
[Slice] The return value of the it->key() and it->value() is a simple structure that contains ...
- C++11用于计算函数对象返回类型的统一方法
[C++11用于计算函数对象返回类型的统一方法] 模板 std::result_of 被TR1 引进且被 C++11 所采纳,可允许我们决定和使用一个仿函数其回返值的类别.底下,CalculusVer ...
- Js中获取frames中的元素
var oCombo = window.parent.frames["frmresourcetype"].document.getElementById('cmbType') ; ...
- 文件拷贝以及base64
File inFile = new File("d:" + File.separator + "test.jpg"); File outFile = new F ...
- Redis初步
Redis目录下文件: redis-server.exe:服务程序 redis-check-dump.exe:本地数据库检查 redis-check-aof.exe:更新日志检查 redis-benc ...
- mysql max_allowed_packet错误
mysql错误 max_allowed_packet 查看目前配置 show VARIABLES like '%max_allowed_packet%'; 修改: set global max_all ...
- JQuery 的bind和unbind函数
测试:页面代码:<body> <input type="button" name="aaa" value="点击我" ...
- Backbone
app.js作为backbone 业务代码主模块,内容很简单,在页面加载完之后,对AppView进行了实例化
- How a non-windowed component can receive messages from Windows -- AllocateHWnd
http://www.delphidabbler.com/articles?article=1 Why do it? Sometimes we need a non-windowed componen ...
- 广州项目实施步骤II_练习配置HaProxy的重定向负载均衡
CentOS6.4 配置Haproxy Haproxy下载地址:http://pan.baidu.com/share/link?shareid=1787182295&uk=18290183 ...