1 public class Foreach { 2 public static void main(String[] args) { 3 Map<String, String> map = new HashMap<String, String>(); 4 5 //循环map的key的Set集合-->不推荐 6 for (String key : map.keySet()) { 7 //key 8 String val = map.get(key); 9 } 10 11 //循环map的entry的Set集合-->最推荐的方式 12 for (Entry<String, String> entry : map.entrySet()) { 13 String key = entry.getKey(); 14 String val = entry.getValue(); 15 } 16 17 //循环map的值-->只取值的推荐方式 18 for (String val : map.values()) { 19 String valOld = val; 20 } 21 } 22 }