Map迭代(六种)

 1 Map<Integer,String> map = new HashMap<Integer,String>();
 2         map.put(1, "a");
 3         map.put(2, "b");
 4         
 5         System.out.println("(1):");
 6         //1.Map输出1(只遍历一次,速度快)
 7         Iterator itr11 = map.entrySet().iterator();
 8         while(itr11.hasNext()){
 9             Map.Entry e = (Map.Entry)itr11.next();
10             System.out.println(e.getKey()+"==="+e.getValue());
11         }
12         System.out.println();
13         System.out.println("(2):");
14         //2.Map输出2(速度慢)
15         Set set = map.keySet();//用接口实例接口
16         Iterator itr22 = set.iterator();
17         while(itr22.hasNext()){//遍历二次,速度慢
18             Integer key = (Integer)itr22.next();
19             System.out.println(key+"==="+map.get(key));
20         }
21         //3.Map输出3(for语句)
22         System.out.println();
23         System.out.println("(3):");
24         Set<Integer> set00 = map.keySet();
25         for(Integer s :set00){
26             System.out.println(s+"==="+map.get(s));
27         }
28         
29         //4.Map输出4
30         System.out.println();
31         System.out.println("(4):");
32         Iterator itr44 = map.values().iterator();        
33         while(itr44.hasNext()){
34             System.out.println(itr44.next());
35         }
36         System.out.println();
37         
38         //5.Map输出5
39         System.out.println("(5):");
40         System.out.println(map);
41         
42         //6.Map输出6
43         System.out.println();
44         System.out.println("(6):");
45         for(Map.Entry en:map.entrySet()){
46             System.out.println(en.getKey()+"==="+en.getValue());
47         }

 

输出:

posted @ 2015-03-22 15:16  金色元年  阅读(437)  评论(0编辑  收藏  举报