- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- public class EgodicMap {
- /**
- * 遍历Map集合
- */
- public static void main(String[] args) {
- // 创建集合
- // Map<String,String> m = new TreeMap<String, String>();
- Map<String, String> m = new HashMap<String, String>();
- // 添加键值对
- m.put("Lily", "18");
- m.put("Lilei", "16");
- m.put("Lucy", "18");
- // 遍历集合 方法一:获取键值Key,再有Key获取Value
- Set<String> s = m.keySet();// 获取所有的键值
- for (String key : s) {
- String value = m.get(key);
- System.out.println(key + "..." + value);
- }
- System.out.println("------------------------------------");
- // 方法二:获取所有的键值对集合,再输出
- Set<Map.Entry<String, String>> ss = m.entrySet();
- for (Map.Entry<String, String> me : ss) {
- String key = me.getKey();
- String value = me.getValue();
- System.out.println(key + "..." + value);
- }
- }
- }
复制代码 |