三种遍历map方式的时间比较

来源:互联网 发布:免费房屋设计软件下载 编辑:程序博客网 时间:2024/06/02 05:31

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
            HashMap<String, String> kmap = new HashMap<String, String>();  
            HashMap<String, String> emap = new HashMap<String, String>();  
            HashMap<String, String> fmap = new HashMap<String, String>(); 
            //装数据  
            for (int i = 0; i < 1000; i++)   
            {  
                kmap.put(""+i, "YL");  
            }  
            for (int i = 0; i < 1000; i++)   
            {  
                emap.put(""+i, "ZT");  
            }  
            for (int i = 0; i < 1000; i++)   
            {  
                fmap.put(""+i, "FK");  
            }  
            long stimes = System.currentTimeMillis();  
            long ctimes = Calendar.getInstance().getTimeInMillis();  
            long dtimes = new Date().getTime();  
               
            //初始时间 这里我用了三种取值方式
            System.out.println(stimes+" "+ctimes+"  "+dtimes); 
  
            //keySet遍历map      
            Iterator<String> ktor = kmap.keySet().iterator();  
            while(ktor.hasNext())  
            {  
                System.out.print(ktor.next() + "=" + kmap.get(ktor.next()) + " ");  
            }  
               
            long stimes1 = System.currentTimeMillis();  
            long ctimes1 = Calendar.getInstance().getTimeInMillis();  
            long dtimes1 = new Date().getTime();     
            System.out.println(stimes1+"    "+ctimes1+" "+dtimes1); 
            System.out.println((stimes1-stimes)+"   "+(ctimes1-ctimes)+"    "+(dtimes1-dtimes));  
             
            //entrySet遍历map 
            Iterator<Entry<String, String>> itor = emap.entrySet().iterator();  
            while(itor.hasNext())  
            {  
                Entry<String, String> e = itor.next();   
                System.out.print(e.getKey() + "=" + e.getValue() + " ");  
            }  
               
            long stimes2 = System.currentTimeMillis();  
            long ctimes2 = Calendar.getInstance().getTimeInMillis();  
            long dtimes2 = new Date().getTime();  
            System.out.println(stimes2+"    "+ctimes2+" "+dtimes2);  
            System.out.println((stimes2-stimes1)+"  "+(ctimes2-ctimes1)+"   "+(dtimes2-dtimes1));  
 
            //for循环遍历 
            for (Map.Entry entry : fmap.entrySet()) 
            
                System.out.print(entry.getKey() + "=" + entry.getValue() + " ");
            }
 
            long stimes3 = System.currentTimeMillis();  
            long ctimes3 = Calendar.getInstance().getTimeInMillis();  
            long dtimes3 = new Date().getTime();  
            System.out.println(stimes3+"    "+ctimes3+" "+dtimes3);  
            System.out.println((stimes3-stimes2)+"  "+(ctimes3-ctimes2)+"   "+(dtimes3-dtimes2));  

输出结果:

原创粉丝点击