首页 > 其他 > 详细

Map循环的三种方法

时间:2015-11-20 12:04:29      阅读:156      评论:0      收藏:0      [点我收藏+]
 1 import java.util.HashMap;
 2 import java.util.Iterator;
 3 import java.util.Map;
 4 
 5 public class MapTest {
 6     public static void main(String[] args){
 7         
 8         Map<String,String> map = new HashMap<String,String>();
 9         map.put("1", "a");
10         map.put("2", "b");
11         map.put("3", "c");
12         
13         //通过Map.keySet()遍历key和value,二次取值
14         for(String key:map.keySet()){
15             System.out.println(key + map.get(key));
16         }
17         
18         //通过Map.entrySet()遍历key和value(推荐使用)
19         for(Map.Entry<String, String> entry:map.entrySet()){
20             System.out.println(entry.getKey() + entry.getValue());
21         }
22         
23         //通过Map.entrySet()使用iterator()遍历key和value
24         Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
25         while(iterator.hasNext()){
26             Map.Entry<String, String> entry = iterator.next();
27             System.out.println(entry.getKey() + entry.getValue());
28         }
29     }
30 }

 

Map循环的三种方法

原文:http://www.cnblogs.com/powerLei/p/4979997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!