Map集合遍历的第二种方法:使用 Entry对象遍历
Map集合中的方法:
Set<Map.Entry<K,V>> entrySet()返回此映射关系的Set视图;
实现步骤:
1、使用Map集合的方法entrySet() 把Map集合多个Entry对象取出来 存储到一个set集合中
2、遍历set集合 获取每一个entry对象
3、使用Entry对象中的方法getKey()和getValue()获取值
public class demo03EntrySet {
public static void main(String[] args) {
Map<String,Integer> map =new HashMap<>();
map.put("xxx",11);
map.put("sss",13);
map.put("www",10);
//把 entry对象取出来 存储到一个set集合中
Set<Map.Entry<String, Integer>> set = map.entrySet();
//遍历set集合 获取每一个entry对象 迭代器遍历
Iterator<Map.Entry<String, Integer>> it = set.iterator();
for(Map.Entry<String,Integer> entry:set){
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"="