@SuppressWarnings("rawtypes") public class HashMapDemo { //hashMap遍历 public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("k1", "v1"); map.put("k2", "v2"); map.put("k3", "v3"); //方法一 Iterator it = map.entrySet().iterator(); while(it.hasNext()){ Map.Entry m = (Map.Entry)it.next(); System.out.println("key=" + m.getKey() + "," + "value=" + m.getValue()); } System.out.println(); //方法二 for(Map.Entry<String, String> m : map.entrySet()){ System.out.println("key=" + m.getKey() + "," + "value=" + m.getValue()); } System.out.println(); //方法三 Iterator iterator = map.keySet().iterator(); while(iterator.hasNext()){ String key = (String)iterator.next(); System.out.println("key=" + key + "," + "value=" + map.get(key)); } } }