//新建map对象
//由于map是接口所以我们新建他的实现类对象
Map<String, String> m = new HashMap<>();
//添加对象
m.put("杨过", "小龙女");
m.put("郭靖", "黄蓉");
m.put("段誉", "王语嫣");
//增强for遍历
Set<String> keys = m.keySet();
for (String key : keys) {
String value = m.get(key);
System.out.println(key + "的妻子是" + value);
}
//迭代器遍历
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
String value = m.get(key);
System.out.println(key + "=" + value);
}
//lambda表达式遍历
m.forEach((key, value) -> {
System.out.println(key + "=" + value);
}
);
Map集合遍历(1)将key存入单列集合
于 2024-08-01 13:56:06 首次发布