29 lines
950 B
Java
29 lines
950 B
Java
package com.inmind.map01;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
/*
|
|
4.Map集合的第二种遍历方式(键值对遍历)
|
|
- public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。
|
|
*/
|
|
public class MapDemo03 {
|
|
public static void main(String[] args) {
|
|
//创建一个键和值都是String的双列集合
|
|
Map<String, String> map = new HashMap<>();
|
|
map.put("刘备", "孙尚香");
|
|
map.put("吕布", "貂蝉");
|
|
map.put("张飞", "夏侯驰");
|
|
System.out.println(map);
|
|
//调用entrySet方法,得到set集合
|
|
Set<Map.Entry<String, String>> entries = map.entrySet();
|
|
//set集合的遍历
|
|
for (Map.Entry<String, String> entry : entries) {
|
|
String key = entry.getKey();
|
|
String value = entry.getValue();
|
|
System.out.println(key+"-"+value);
|
|
}
|
|
}
|
|
}
|