[20-05-22][Thinking in Java 36]Java Container 8 - Map - 1

 1 package test_19_2;
 2 
 3 import java.util.HashMap;
 4 import java.util.Iterator;
 5 import java.util.Map;
 6 
 7 public class MapTest {
 8     
 9     public static void main(String[] args) {
10         
11         Map<String, String> gerbil = new HashMap<String, String>();
12         gerbil.put("1", "Fuzzy");
13         gerbil.put("2", "Spot");
14         gerbil.put("3", "Mouth");
15         
16         // 为所有键组成的Set获取Iterator
17         Iterator<String> it = gerbil.keySet().iterator();
18         
19         // 使用Iterator遍历获取所有键对应的值
20         while (it.hasNext()) {
21             System.out.println(gerbil.get(it.next()));
22         }
23         
24     }
25 }

结果如下:

Fuzzy
Spot
Mouth

猜你喜欢

转载自www.cnblogs.com/mirai3usi9/p/12937946.html