public class HashMapDemo {
public static void main(String[] args) {
//创建Map对象
HashMap<String,String> hm = new HashMap<String,String>();
//添加映射关系
hm.put("ITCAST001", "张三");
hm.put("ITCAST002", "李四");
hm.put("ITCAST003", "王五");
hm.put("ITCAST003", "赵六");
//遍历Map对象
//方式1 获取所有的key,通过key来获取value
Set<String> keys = hm.keySet();
for (String key : keys) {
String value = hm.get(key);
System.out.println(key + "=" + value);
}
System.out.println("------------------");
//方式2:获取所有的结婚证对象,然后通过结婚证对象获取丈夫和媳妇
Set<Map.Entry<String, String>> entrys = hm.entrySet();
for (Map.Entry<String, String> entry : entrys) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + "=" + value);
}
}
}
遍历Map集合的两种方式
最新推荐文章于 2022-02-24 14:15:59 发布