Map的迭代

 1 public static void main(String[] args) {
 2 
 3         Map<String, Integer> map = new HashMap<String, Integer>();
 4         map.put("张三", 12);
 5         map.put("张三1", 121);
 6         map.put("张三2", 122);
 7         map.put("张三3", 123);
 8         map.put("张三4", 124);
 9         map.put("张三5", 125);
10         map.put("张三6", 126);
11         map.put("张三7", 127);
12 
13         // keySet
14         Set<String> set = map.keySet();
15         Iterator<String> it = set.iterator();
16         while (it.hasNext()) {
17             String key = it.next();
18             System.out.println(key + "---" + map.get(key));
19         }
20         System.out.println("==============================================");
21         // foreach 遍历keySet
22         for (String string : map.keySet()) {
23             System.out.println(string + "===" + map.get(string));
24         }
25 
26         System.out.println("==============================================");
27 
28         // entrySet
29         Set<Entry<String, Integer>> setenty = map.entrySet();
30         Iterator<Entry<String, Integer>> ite = setenty.iterator();
31         while (ite.hasNext()) {
32             Entry<String, Integer> m = ite.next();
33             System.out.println(m.getKey() + "---" + m.getValue());
34         }
35         
36         System.out.println("==============================================");
37         
38         // foreach 遍历entrySet
39         for (Entry<String, Integer> entry : map.entrySet()) {
40             System.out.println(entry.getKey() + "===" + entry.getValue());
41         }
42     }

 

posted @ 2017-04-12 17:22  倾默  阅读(270)  评论(0编辑  收藏  举报