Map的高效遍历

场景:偶尔生产环境的某台机器CPU使用率很高,经过定位发现是有一个大的HashMap(HashMap里面存放了大量数据,比如1W条)做循环引起的。

代码中采用了如下的遍历

 

Java代码   收藏代码
  1. for(Iterator ite = map.keySet().iterator(); ite.hasNext();){  
  2.   Object key = ite.next();  
  3.   Object value = map.get(key);  
  4. }  

 

  通过Map类的get(key)方法获取value时,会进行两次hashCode的计算,消耗CPU资源;而使用entrySet的方式,map对象会直接返回其保存key-value的原始数据结构对象,遍历过程无需进行错误代码中耗费时间的hashCode计算;这在大数据量下,体现的尤为明显。

以下是HashMap.get()方法的源码:

Java代码   收藏代码
  1. public V get(Object key) {  
  2.         if (key == null)  
  3.             return getForNullKey();  
  4.         int hash = hash(key.hashCode());  
  5.         for (Entry<K,V> e = table[indexFor(hash, table.length)];  
  6.              e != null;  
  7.              e = e.next) {  
  8.             Object k;  
  9.             if (e.hash == hash && ((k = e.key) == key || key.equals(k)))  
  10.                 return e.value;  
  11.         }  
  12.         return null;  
  13.     }  

 

 

正确用法如下:

 

Java代码   收藏代码
  1. for(Iterator ite = map.entrySet().iterator(); ite.hasNext();){  
  2.   Map.Entry entry = (Map.Entry) ite.next();  
  3.   entry.getKey();  
  4.   entry.getValue();  
  5. }  

 

 

对比测试

 

Java代码   收藏代码
  1. public class HashMapTest {  
  2.     public static void getFromMap(Map map){  
  3.         for(Iterator ite = map.keySet().iterator(); ite.hasNext();){  
  4.             Object key = ite.next();  
  5.             Object value = map.get(key);  
  6.         }  
  7.     }  
  8.     public static void getFromMapByEntrySet(Map map){  
  9.         for(Iterator ite = map.entrySet().iterator(); ite.hasNext();){  
  10.             Map.Entry entry = (Map.Entry) ite.next();  
  11.             entry.getKey();  
  12.             entry.getValue();  
  13.         }  
  14.     }  
  15.   
  16.     public static void main(String[] args) {  
  17.         Map map = new HashMap();  
  18.         for(int i=0;i<200000;i++){  
  19.             map.put("key"+i, "value"+i);  
  20.         }  
  21.         long currentTime = System.currentTimeMillis();  
  22.         getFromMap(map);  
  23.         long currentTime2 = System.currentTimeMillis();  
  24.         getFromMapByEntrySet(map);  
  25.         long currentTime3 = System.currentTimeMillis();  
  26.         System.out.println(currentTime2-currentTime);  
  27.         System.out.println(currentTime3-currentTime2);  
  28.     }  
  29.   
  30. }  

 

运行结果:

 

 

 

Java代码   收藏代码
  1. 16  
  2. 0  

 

 

经过对比,可以看到明显的差别。

还有一种最常用的遍历方法,其效果也不太好,不建议使用

 

Java代码   收藏代码
  1. for(Iterator i = map.values().iterator(); i.hasNext();)   {         
  2.             Object temp =  i.next();     
  3. }    

 


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值