Map遍历效率比较

1、由来     

      上次博客提到了Map的四种遍历方法,其中有的只是获取了key值或者是value值,但我们应该在什么时刻选择什么样的遍历方式呢,必须通过实践的比较才能看到效率。

        也看了很多文章,大家建议使用entrySet,认为entrySet对于大数据量的查找来说,速度更快,今天我们就通过下面采用不同方法遍历key+value,key,value不同情景下的差异。


2、准备测试数据:

      HashMap1:大小为1000000,key和value的值均为String,key的值为1、2、3.........1000000;

      

[html]  view plain  copy
  1. Map<String,String> map =new HashMap<String,String>();  
  2.     String key,value;  
  3.   
  4.     for(int i=1;i<=num;i++){  
  5.         key = ""+i;  
  6.         value="value"+i;  
  7.         map.put(key,value);  
  8.     }  
      

      HashMap2:大小为1000000,key和value的值为String,key的值为50、100、150........50000000;

     

[html]  view plain  copy
  1. Map<String,String> map = new HashMap<String,String>();  
  2.    String key,value;  
  3.   
  4.    for(int i=1;i<=num;i++){  
  5.        key=""+(i*50);  
  6.        value="value"+key;  
  7.        map.put(key,value);  
  8. }  

扫描二维码关注公众号,回复: 36635 查看本文章

3、场景测试

    3.1遍历key+value

      1)keySet利用Iterator遍历

          

[html]  view plain  copy
  1. long startTime1 =System.currentTimeMillis();  
  2. Iterator<String> iter = map.keySet().iterator();  
  3. while (iter.hasNext()){  
  4.       key=iter.next();  
  5.       value=map.get(key);  
  6. }  
  7. long endTime1 =System.currentTimeMillis();  
  8. System.out.println("第一个程序运行时间:"+(endTime1-startTime1)+"ms");  

     2)keySet利用for遍历

     

[html]  view plain  copy
  1. long startTime2 =System.currentTimeMillis();  
  2.   for(String key2:map.keySet()){  
  3.       value=map.get(key2);  
  4.   }  
  5.   long endTime2 =System.currentTimeMillis();  
  6.   System.out.println("第二个程序运行时间:"+(endTime2-startTime2)+"ms");  

     3)entrySet利用Iterator遍历

[html]  view plain  copy
  1. long startTime3=System.currentTimeMillis();  
  2.         Iterator<Map.Entry<String,String>> iter3 =map.entrySet().iterator();  
  3.         Map.Entry<String,String> entry3;  
  4.         while (iter3.hasNext()){  
  5.             entry3 = iter3.next();  
  6.             key = entry3.getKey();  
  7.             value=entry3.getValue();  
  8.         }  
  9.         long endTime3 =System.currentTimeMillis();  
  10.         System.out.println("第三个程序运行时间:" +(endTime3-startTime3)+"ms");  


      4)entrySet利用for遍历

  

[html]  view plain  copy
  1. long startTime4=System.currentTimeMillis();  
  2.         for(Map.Entry<String,String> entry4:map.entrySet()){  
  3.             key=entry4.getKey();  
  4.             value=entry4.getValue();  
  5.         }  
  6.         long endTime4 =System.currentTimeMillis();  
  7.         System.out.println("第四个程序运行时间:"+(endTime4-startTime4) +"ms");  

     3.2遍历key

       1)keySet利用Iterator遍历

           

[html]  view plain  copy
  1. long startTime1 =System.currentTimeMillis();  
  2.         Iterator<String> iter = map.keySet().iterator();  
  3.         while (iter.hasNext()){  
  4.             key=iter.next();  
  5.   
  6.         }  
  7.         long endTime1 =System.currentTimeMillis();  
  8.         System.out.println("第一个程序运行时间:"+(endTime1-startTime1)+"ms");  

        2)keySet利用for遍历

[html]  view plain  copy
  1. long startTime2 =System.currentTimeMillis();  
  2.         for(String key2:map.keySet()){  
  3.   
  4.         }  
  5.         long endTime2 =System.currentTimeMillis();  
  6.         System.out.println("第二个程序运行时间:"+(endTime2-startTime2)+"ms");  

        3)entrySet利用Iterator遍历

   

[html]  view plain  copy
  1. long startTime3=System.currentTimeMillis();  
  2.        Iterator<Map.Entry<String,String>> iter3 =map.entrySet().iterator();  
  3.        Map.Entry<String,String> entry3;  
  4.        while (iter3.hasNext()){  
  5.            key = iter3.next().getKey();  
  6.   
  7.        }  
  8.        long endTime3 =System.currentTimeMillis();  
  9.        System.out.println("第三个程序运行时间:" +(endTime3-startTime3)+"ms");  

        4)entrySet利用for遍历

[html]  view plain  copy
  1. long startTime4=System.currentTimeMillis();  
  2.        for(Map.Entry<String,String> entry4:map.entrySet()){  
  3.            key=entry4.getKey();  
  4.        }  
  5.        long endTime4 =System.currentTimeMillis();  
  6.        System.out.println("第四个程序运行时间:"+(endTime4-startTime4) +"ms");  

     

     3.3遍历value


        1)keySet利用Iterator遍历

[html]  view plain  copy
  1. long startTime1 =System.currentTimeMillis();  
  2.         Iterator<String> iter = map.keySet().iterator();  
  3.         while (iter.hasNext()){  
  4.            value=map.get(iter.next());  
  5.         }  
  6.         long endTime1 =System.currentTimeMillis();  
  7.         System.out.println("第一个程序运行时间:"+(endTime1-startTime1)+"ms");  

            2)keySet利用for遍历

[html]  view plain  copy
  1. long startTime2 =System.currentTimeMillis();  
  2.        for(String key2:map.keySet()){  
  3.            value=map.get(key2);  
  4.        }  
  5.        long endTime2 =System.currentTimeMillis();  
  6.        System.out.println("第二个程序运行时间:"+(endTime2-startTime2)+"ms");  

          3)entrySet利用Iterator遍历

[html]  view plain  copy
  1. long startTime3=System.currentTimeMillis();  
  2.        Iterator<Map.Entry<String,String>> iter3 =map.entrySet().iterator();  
  3.        Map.Entry<String,String> entry3;  
  4.        while (iter3.hasNext()){  
  5.           value=iter3.next().getValue();  
  6.   
  7.        }  
  8.        long endTime3 =System.currentTimeMillis();  
  9.        System.out.println("第三个程序运行时间:" +(endTime3-startTime3)+"ms");  

           4)entrySet利用for遍历

      

[html]  view plain  copy
  1. long startTime4=System.currentTimeMillis();  
  2.         for(Map.Entry<String,String> entry4:map.entrySet()){  
  3.             value=entry4.getValue();  
  4.         }  
  5.         long endTime4 =System.currentTimeMillis();  
  6.         System.out.println("第四个程序运行时间:"+(endTime4-startTime4) +"ms");  

         5)values利用iterator遍历

  

[html]  view plain  copy
  1. long startTime5=System.currentTimeMillis();  
  2.        Iterator<String>  iter5=map.values().iterator();  
  3.        while (iter5.hasNext()){  
  4.            value=iter5.next();  
  5.        }  
  6.        long endTime5 =System.currentTimeMillis();  
  7.        System.out.println("第五个程序运行时间:"+(endTime5-startTime5) +"ms");  

       6)values利用for遍历

 

[html]  view plain  copy
  1. long startTime6=System.currentTimeMillis();  
  2.        for(String value6:map.values()){  
  3.   
  4.        }  
  5.        long endTime6 =System.currentTimeMillis();  
  6.        System.out.println("第六个程序运行时间:"+(endTime6-startTime6) +"ms");  


4、时间对比

     4.1遍历key+value

       

遍历内容:key+value

HashMap1

HashMap2

keySet用iterator遍历

56

93

Keyset用for遍历

50

91

entrySet用iterator遍历

40

70

entrySet用for遍历

41

73


     4.2遍历key

  

           

遍历内容:key

HashMap1

HashMap2

keySet用iterator遍历

37

58

Keyset用for遍历

34

57

entrySet用iterator遍历

38

56

entrySet用for遍历

34

58


      4.3遍历value

遍历内容:value

HashMap1

HashMap2

keySet用iterator遍历

65

86

Keyset用for遍历

54

80

entrySet用iterator遍历

42

61

entrySet用for遍历

44

61

Values用iterator遍历

37

63

Values用for遍历

47

54

5、总结

      从上面的时间比较来看:

      1)map的key采用简单形式和复杂形式时,查找的效率是不同的,简单的key值效率更高

      2)当数据量大的时候,采用entrySet遍历key+value的效率要高于keySet

      3)当我们只需要取得value值时,采用values来遍历效率更高

转载:https://blog.csdn.net/zhangsify/article/details/52966094

猜你喜欢

转载自blog.csdn.net/q89757316/article/details/80020344
今日推荐