HashMap的几种遍历方法
1、第一种:
Map<String, ArrayList> map = new HashMap<String, ArrayList>(); Set<String> keys = map.keySet(); Iterator<String> iterator = keys.iterator(); while (iterator.hasNext()) { String key = iterator.next(); ArrayList arrayList = map.get(key); for (Object o : arrayList) { System.out.println(o + "遍历过程"); } }
2、第二种:
Map<String, Integer> tempMap = new HashMap<String, Integer>(); tempMap.put("a", 1); tempMap.put("b", 2); tempMap.put("c", 3); for (String o : tempMap.keySet()) { System.out.println("key=" + o + " value=" + tempMap.get(o)); }
3、第三种:
Map<String, Integer> tempMap = new HashMap<String, Integer>(); tempMap.put("a", 1); tempMap.put("b", 2); tempMap.put("c", 3); for (Iterator i = tempMap.keySet().iterator(); i.hasNext();) { String obj = i.next(); System.out.println(obj);// 循环输出key System.out.println("key=" + obj + " value=" + tempMap.get(obj)); }