遍历hashMap对效率的影响
测试环境:jdk1.7.0_79\Processor 1.7 GHz Intel Core i5
遍历Map的方式有很多,通常场景下我们需要的是遍历Map中的Key和Value。
更新:增加一个方法对方法一优化,采用foreach循环
写了四个方法:
/**
* while循环 map.entrySet().iterator()获取map的value
* @param map
*/
public static void getMap1(Map<String,String> map){
Iterator<Map.Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
}
}
/**
* while循环 map.keySet().iterator()获取map的value
* @param map
*/
public static void getMap2(Map<String,String> map){
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String value = map.get(key);
}
}
/**
* foreach循环map.entrySet()获取map的value
* getMap1的另一种写法,采用foreach循环.foreach循环的底层实现原理就是迭代器Iterator
* @param map
*/
public static void getMap3(Map<String,String> map){
Set<Map.Entry<String, String>> set = map.entrySet();
for (Map.Entry<String, String> entry : set) {
String value = entry.getValue();
}
}
/**
* foreach循环map.keySet()获取map的value
* @param map
*/
public static void getMap4(Map<String,String> map){
Set<String> set = map.keySet();
for (String entry : set) {
String value = map.get(entry);
}
}
注:我以前常用的就是方法2,原则就是能用就行。所以第一印象不一定对哦。
默认map中已经存储了数据,map数据量不确定,用户访问量不确定
我采用for循环多次调用getMap方法来模拟用户访问,如下:
HashMap<String, String> map = new HashMap<String, String>();
int mapNum=1000;//设置map数据量
int forNum = 1000;//设置for循环调用次数,模拟用户访问次数
//模拟map已经存储了数据
for (int i = 0; i < mapNum; i++) {
map.put("key" + i, "value" + i);
}
//用for循环调用getMap来模拟多次访问
long startTime = System.currentTimeMillis();
for (int i = 0; i < forNum; i++) {
getMap1(map);
}
System.out.println("getMap1:"+(System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
for (int i = 0; i < forNum; i++) {
getMap2(map);
}
System.out.println("getMap2:"+(System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
for (int i = 0; i < forNum; i++) {
getMap3(map);
}
System.out.println("getMap3:"+(System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
for (int i = 0; i < forNum; i++) {
getMap4(map);
}
System.out.println("getMap4:"+(System.currentTimeMillis() - startTime) + "ms");
(以下测试数据是前三个方法的数据,方法四也类似的结果)
map数据量为10,用户访问10次(for循环调用getMap 10次)
看不出来效果
map数据量为10,用户访问100次(for循环调用getMap 100次)
看不出来效果
map数据量为10,用户访问1000次(for循环调用getMap 1000次)
getMap1:15ms
getMap2:22ms
getMap3:4ms
map数据量为10,用户访问10000次(for循环调用getMap 10000次)
getMap1:27ms
getMap2:31ms
getMap3:15ms
map数据量为10,用户访问100000次(for循环调用getMap 10万次)
getMap1:52ms
getMap2:53ms
getMap3:32ms
map数据量为10,用户访问1000000次(for循环调用getMap 百万次)
getMap1:169ms
getMap2:280ms
getMap3:128ms
百万级别的时候差距拉大了,但有时候1和3差距不大
map数据量为100,for循环调用getMap 100次
getMap1:8ms
getMap2:18ms
getMap3:4ms
一倍差距啊
map数据量为100,for循环调用getMap 1000次
getMap1:23ms
getMap2:28ms
getMap3:14ms
map数据量为100,for循环调用getMap 1万次
getMap1:38ms
getMap2:57ms
getMap3:28ms
map数据量为100,for循环调用getMap 10万次
getMap1:126ms
getMap2:221ms
getMap3:75ms
map数据量为100,for循环调用getMap 百万次
getMap1:641ms
getMap2:1823ms
getMap3:574ms
1和2三倍啊!,不过这个时候1和3差距不大了
后面仅仅是摘选几条:
map数据量为1000,for循环调用getMap 1000次
getMap1:44ms
getMap2:43ms
getMap3:26ms
map数据量为1000,for循环调用getMap 是万次
getMap1:1259ms
getMap2:2420ms
getMap3:887ms
map数据量为1000,for循环调用getMap 百万次
getMap1:11210ms
getMap2:21083ms
getMap3:6941ms
map数据量为10000,for循环调用getMap 10000次
getMap1:2807ms
getMap2:4770ms
getMap3:2511ms
map数据量为10000,for循环调用getMap 百万次
getMap1:167107ms
getMap2:230967ms
getMap3:123263ms
基本上方法一的效率和四差不多,比方法二高一倍,方法三比方法一效率高一些,建议采用方法三。当然实际情况可能很复杂,硬件设备、并发情况,更多的时候仅仅是map数据量大但是访问量小,甚至可能我们采用服务器启动即加载的方式避免了用户访问时遍历。
而且要注意,不同情况下的写法不同,测试结果可能是不一样的。需要注重的是,一旦涉及到遍历的情况,就要考虑数据量和访问量,来选择最佳的遍历方式。
顺便提一句:有些人可能习惯这样写代码
for(int i = 0;i < list.length();i++){}以及foreach语句,我印象中for循环中每次都要计算一次list.length,这也是不必要的开销。foreach在顺序访问中效率高,所以如果是遍历arrayList的时候不要用foreach
更新:如果只是想要遍历一下key呢?方法三不一定比方法二好,方法一肯定比方法二差。例如
map数据量为10000,for循环调用getMap 10000次
getMap1:2767ms
getMap2:1068ms
getMap3:2208ms
根据实际情况选择吧。
欢迎各位补充。
遍历hashMap对效率的影响的更多相关文章
- java遍历Hashmap/Hashtable的几种方法
一>java遍历Hashtabe: import java.util.Hashtable; import java.util.Set; public class HashTableTest { ...
- java遍历hashMap、hashSet、Hashtable
一.遍历HashMap Map<Integer, String> map = new HashMap<Integer, String>(); 方法一:效率高 for(Entry ...
- java 中遍历hashmap 和hashset 的方法
一.java中遍历hashmap: for (Map.Entry<String, Integer> entry : tempMap.entrySet()) { String ...
- 使用多种方式实现遍历HashMap
今天讲解的主要是使用多种方式来实现遍历HashMap取出Key和value,首先在java中如果想让一个集合能够用for增强来实现迭代,那么此接口或类必须实现Iterable接口,那么Iterable ...
- [Java] 遍历HashMap和HashMap转换成List的两种方式
遍历HashMap和HashMap转换成List /** * convert the map to the list(1) */ public static void main(String[] ...
- 专题三、ArrayList遍历方式以及效率比较
一.遍历方式 ArrayList支持三种遍历方式. 1.第一种,随机访问,它是通过索引值去遍历 由于ArrayList实现了RandomAccess接口,它支持通过索引值去随机访问元素. 代码如下: ...
- Java遍历HashMap并修改(remove)(转载)
遍历HashMap的方法有多种,比如通过获取map的keySet, entrySet, iterator之后,都可以实现遍历,然而如果在遍历过程中对map进行读取之外的操作则需要注意使用的遍历方式和操 ...
- 四种遍历hashMap的方法及比较
学习怎样遍历Java hashMap及不同方法的性能. // hashMap的遍历 public void testHashMap() { Map<String, String> map ...
- OC各种遍历方法的效率比较
看了一篇博客,挺有意思,OC各种遍历方法的效率,打算自己也测试一番.看看,究竟哪一个的效率更好一些! 准备工作:懒加载一个数组,创建一千万个对象添加到数组. #pragma mark - Lazy M ...
随机推荐
- IOS开发之IOS8.0最新UIAlertController 分类: ios技术 2015-01-20 14:24 144人阅读 评论(1) 收藏
最近苹果更新的IOS8 对以前进行了很大的修改, 更新的API也让人捉急,据说iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.比如全新的UIPrese ...
- [转]配置 VIM 的 Go 语言开发环境
本文是针对像我这样的 VIM 小白而写的,所使用的 VIM-GO 插件虽然步骤简单但不够详细,特写此文以做记录和分享.欢迎各位大神纠正补充! 特别说明 本博文不是 Go 语言环境搭建教程,只是 VIM ...
- node.js 下依赖Express 实现post 4种方式提交参数
上面这个图好有意思啊,哈哈, v8威武啊.... 在2014年的最后一天和大家分享关于node.js 如何提交4种格式的post数据. 上上一篇说到了关于http协议里定义的4种常见数据的post方法 ...
- 利用curl验证ssl网站(webservice)
curl的用法: http://linux.about.com/od/commands/l/blcmdl1_curl.htm DER格式的cert转换为PEM格式(curl只接受PEM格式): ope ...
- Swift迁入第三方库时的版本错误解决
我的swift的项目用的是swift 2.3的版本,但是用CocoaPods迁入一个第三方:ObjectMapper后,编译会出现这样一个问题: Use Legacy Swift Language V ...
- 刷新UITableView
[from]http://www.superqq.com/blog/2015/08/18/ios-development-refresh-uitableview/ UITableView对于iOS开发 ...
- linux下安装mysql(编译mysql源码)
编译所需软件地址 http://mysql.mirror.kangaroot.net/Downloads/ -- 下载需要的mysql版本例如mysql-5.5.39.tar.gz 目前还不太 ...
- TCP carries HTTP data in order, and without corruption
wTCP/IP承载几乎所有的HTTP通信. HTTP The Definitive Guide
- Spring Boot Web Executable Demo
Spring Boot Web Executable Demo */--> pre.src {background-color: #292b2e; color: #b2b2b2;} pre.sr ...
- IOS web app一些实用的属性设置
IOS对safari私有的属性很多,虽然很多不为人知但是却很实用.掌握好这些属性对web app和混合app的开发会很有帮助. 1.format-detection[telephone=no] 是否自 ...