首页 > 其他 > 详细

Map 遍历的几种方法

时间:2017-07-28 16:14:42      阅读:165      评论:0      收藏:0      [点我收藏+]

复习map的过程中想到的,做个简单的记录

 1 public class HashMapTest {
 2 
 3     public static void main(String args[]) {
 4         Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
 5         hm.put(1, 8);
 6         hm.put(2, 7);
 7         hm.put(3, 6);
 8         hm.put(4, 5);
 9         System.out.println(hm);
10         System.out.println("第一种:foreach循环");
11         for (Integer i : hm.keySet()) {
12             Integer a = hm.get(i);
13             System.out.println(a);
14         }
15 
16         System.out.println("第二种:迭代器");
17         Iterator<Map.Entry<Integer, Integer>> it = hm.entrySet().iterator();
18         while (it.hasNext()) {
19             System.out.println(it.next().getValue());
20         }
21 
22         System.out.println("第三种:");
23         for (Map.Entry<Integer, Integer> entry : hm.entrySet()) {
24             System.out.println(entry.getKey() + "--" + entry.getValue());
25         }
26 
27     }
28 
29 }

 

Map 遍历的几种方法

原文:http://www.cnblogs.com/wowind/p/7250819.html

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