遍历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 ...
随机推荐
- X-008 FriendlyARM tiny4412 uboot移植之copy u-boot到DDR内存
<<<<<<<<<<<<<<<<<<<<<<<<< ...
- VB.NET中网络编程的另一种方案----system.net中的HttpWebRequest类的使用
VB.NET中网络编程的另一种方案---- system.net中的HttpWebRequest类的使用 在VB.net中进行网络编程,除了我之前写的随笔中的使用WinHttp组件进行编程,还有另一种 ...
- IOS 实现TXT文本自动识别编码的方法
from :http://kyoworkios.blog.51cto.com/878347/1344013 TXT识别编码是个复杂的问题.幸好有c/c++的一个库能识别. 库的叫uchardet,可以 ...
- HDU 2830:Matrix Swapping II(思维)
http://acm.hdu.edu.cn/showproblem.php?pid=2830 题意:-- 思路:对于每一列,它是固定的,用dp[][]处理出连续的长度.例如: 假设我们扫第四列的时候, ...
- SVN下载分支、合并分支
http://www.cnblogs.com/armyfai/p/3985660.html(不知道实际工作中怎么使用,有好多步骤感觉不是太理解) 从远处把库fork到本地,会有一个库文件夹 服务器库结 ...
- 315.Count of Smaller Numbers After Self My Submissions Question
You are given an integer array nums and you have to return a new counts array. Thecounts array has t ...
- K-Means 算法(转载)
K-Means 算法 在数据挖掘中, k-Means 算法是一种 cluster analysis 的算法,其主要是来计算数据聚集的算法,主要通过不断地取离种子点最近均值的算法. 问题 K-Means ...
- iOS 之 内存管理
凡是alloc copy mutablecopy init 声明的变量,都需要通过手动的方式进行释放,realse. 如果 copy一个对象,则拥有了拷贝的对象,要负责释放. 如果 保持(retain ...
- 一张图告诉你什么是Linux distributions...
- SecureCRT 使用
1. 连接linux 服务器 http://www.cnblogs.com/SimonGao/p/4959274.html