Java中遍历Map的两种方法:keySet和entrySet

时间:2022-05-10 19:36:22
  //方法一  
  Set<String> set = map.keySet();   
  for (String s:set) {  
   System.out.println(s+","+map.get(s));  
  }  
  //方法二:将Map集合中的映射关系(Map.Entry类型的)取出,存入到Set集合中  
  Set<Map.Entry<String, String>> entryseSet=map.entrySet();  
  for (Map.Entry<String, String> entry:entryseSet) {  
   System.out.println(entry.getKey()+","+entry.getValue());  
  }  

  

 1 import java.util.Calendar;
 2 import java.util.Date;
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map.Entry;
 6 
 7 /**
 8  * 测试keySet()与entrySet()的迭代时间
 9  * keySet():迭代后只能通过get()取key
10  * entrySet():迭代后可以e.getKey(),e.getValue()取key和value。返回的是Entry接口
11  * 最后说明下keySet()的速度比entrySet()慢了很多。看来以后要考虑用entrySet()了
12  * @author JackeenLiu
13  * @date 2014-4-14 星期一
14  */
15 public class HashMapTest         
16 {
17         public static void main(String[] args) 
18         {
19                 HashMap<String,String> kmap = new HashMap<String,String>();
20                 HashMap<String, String> emap = new HashMap<String, String>();
21                 
22                 //装数据
23                 for (int i = 0; i < 100; i++) 
24                 {
25                         kmap.put(""+i, "abcdefghijklmnopqrstuvwxyz");
26                 }
27                 for (int i = 0; i < 100; i++) 
28                 {
29                         emap.put(""+i, "abcdefghijklmnopqrstuvwxyz");
30                 }
31                 
32                 long stimes = System.currentTimeMillis();
33                 long ctimes = Calendar.getInstance().getTimeInMillis();
34                 long dtimes = new Date().getTime();
35                 
36                 //初始时间 这里我用了三种取值方式 最后发现System.currentTimeMillis();是最直接的取值方法
37                 System.out.println(stimes+"        "+ctimes+"        "+dtimes);
38                 
39                 Iterator<String> ktor = kmap.keySet().iterator();
40                 while(ktor.hasNext())
41                 {
42                         System.out.println(ktor.next());
43                 }
44                 
45                 long stimes1 = System.currentTimeMillis();
46                 long ctimes1 = Calendar.getInstance().getTimeInMillis();
47                 long dtimes1 = new Date().getTime();
48                 
49                 //结束世界并且也是entrySet的开始时间
50                 System.out.println((stimes1-stimes)+"        "+(ctimes1-ctimes)+"        "+(dtimes1-dtimes));
51                 System.out.println(stimes1+"        "+ctimes1+"        "+dtimes1);
52                 
53                 Iterator<Entry<String, String>> itor = emap.entrySet().iterator();
54                 while(itor.hasNext())
55                 {
56                         Entry<String, String> e = itor.next();
57                         //System.out.println(e.getKey());
58                         System.out.println(e.getValue());
59                 }
60                 
61                 long stimes2 = System.currentTimeMillis();
62                 long ctimes2 = Calendar.getInstance().getTimeInMillis();
63                 long dtimes2 = new Date().getTime();
64                 System.out.println(stimes2+"        "+ctimes2+"        "+dtimes2);
65                 System.out.println((stimes2-stimes1)+"        "+(ctimes2-ctimes1)+"        "+(dtimes2-dtimes1));
66         }
67 }