Map集合的遍历.

 1 package collction.map;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 import java.util.Set;
 7 
 8 public class Demo_1 {
 9     public static void main(String[] args) {
10         mapIterator();
11     }
12   public static void mapIterator(){
13       Map<Integer, String> map = new HashMap<>();
14       map.put(1, "abc");
15       map.put(2, "cdf");
16 //      使用map中的keset()方法 返回一个含有键的set集合
17      Set<Integer> set =map.keySet();
18 //     遍历Set中的集合,获取Set集合中的所有的元素
19      Iterator<Integer> it =set.iterator();
20      while(it.hasNext()){
21         Integer key = it.next();
22 //        调用Map集合中的方法get()通过键获取值。
23         String value =map.get(key);
24         System.out.println(key+"..."+value);
25         
26          
27      }
28   }
29 }

猜你喜欢

转载自www.cnblogs.com/hnwxp/p/10638172.html