package com.oracle.demo01; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Demo01 { public static void main(String[] args) { //HashMap 存储自定义类型键值 HashMap<String, String> map=new HashMap<String, String>(); //存值 map.put("张三", "123"); map.put("小红", "123"); map.put("张三", "456"); //删除 //map.remove("张三"); //取值 //System.out.println(map.get("张三")); //遍历 //1.keySet+增强for 遍历 //获取所有key所在的Set集合 Set<String> set =map.keySet(); for (String key : set) { System.out.println(map.get(key)); } //2.keySet+iterator 遍历 Iterator<String> it=set.iterator(); while (it.hasNext()) { String k=it.next(); String value=map.get(k); //打印key+value System.out.println(k+" "+value); } } }
.