遍历Map的方法

1、使用 entrySet()

entrySet()  -->  官方推荐
将Map转换成Map.Entry对象的Set集合
Set entrys = map.entrySet();
Iterator it = entrys.iterator();
    while(it.hasNext()){
         //Map 的 内部类
        Entry entry = (Entry) it.next();
  System.out.println(entry.getKey()+" = "+entry.getValue());       
    }
 

2、使用 keySet()

keySet()  先获得Map中所有的key的 set 集合
 Set keys = map.keySet();
 Iterator it = keys.iterator();
    while(it.hasNext()){
        String key = (String) it.next();
        System.out.println(key+" = "+map.get(key));
    }
注意:这里面涉及到泛型的使用,用Eclpise编程的时候会出现警告,只要按照你的需要添加泛型就可以了
原文地址:https://www.cnblogs.com/tyzl/p/5515102.html