美文网首页
HashMap最快遍历方法

HashMap最快遍历方法

作者: 梦沉薇露 | 来源:发表于2016-08-17 17:59 被阅读0次

后来我发现下面这篇特完美!
http://www.cnblogs.com/meieiem/archive/2011/11/02/2233041.html

经过本人多伦测试:
排行1:采用键值对查找最快(测试代码方法二)
排行2:其次才是结合Iterator 的(测试代码方法一)
排行3:最次就是看开发人员有多蠢了

以下附上测试代码:

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.*;
import java.util.Map.Entry;
public class test  
{
    public static void main (String[] args) throws java.lang.Exception
    {   
        
        System.out.println(System.currentTimeMillis());
        HashMap<String,String> map = new HashMap<String,String>();
        for (int i=1;i<10000 ; i++) {
            
            map.put(i+"", i+"");

        }
        
        long currnt = System.currentTimeMillis() ;
        //方法一
        Iterator iterator = map.entrySet().iterator();
        while(iterator.hasNext()){     
           Map.Entry<String, String> entry= (Entry<String, String>) iterator.next();
           System.out.println("1");
            //System.out.println("key:"+entry.getKey()+" value"+entry.getValue()); 
        }      
        
        long curen2 = System.currentTimeMillis();
        System.out.println(curen2 -currnt);
        //方法二:
        for (Map.Entry<String, String> m : map.entrySet()) {
            System.out.println("1");
          // System.out.println("key:"+m.getKey()+" value"+m.getValue());
          }
          
        System.out.println(System.currentTimeMillis()-curen2 );
      

    } 

}


相关文章

网友评论

      本文标题:HashMap最快遍历方法

      本文链接:https://www.haomeiwen.com/subject/cegmsttx.html