创建一个MAP的栗子:
1 MaptempMap = new HashMap ();2 tempMap.put("abc", 1);3 tempMap.put("aoe", 2);4 tempMap.put("xyz", 3);
方法一:
1 Iterator> it = tempMap.entrySet().iterator();2 while (it.hasNext()) {3 Entry entry = it.next();4 Object key = entry.getKey();5 Object value = entry.getValue();6 System.out.println("key=" + key + " value=" + value);7 }
方法二:
1 for (Map.Entryentry : tempMap.entrySet()) {2 String key = entry.getKey().toString();3 String value = entry.getValue().toString();4 System.out.println("key=" + key + " value=" + value);5 }
方法三:
1 for (Iteratori = tempMap.keySet().iterator(); i.hasNext();) {2 Object obj = i.next();3 System.out.println(obj);4 System.out.println("key=" + obj + " value=" + tempMap.get(obj));5 }6 for (Iterator i = tempMap.values().iterator(); i.hasNext();) {7 Object obj = i.next();8 System.out.println(obj);9 }
方法四:
1 for (Object o : tempMap.keySet()) {2 System.out.println("key=" + o + " value=" + tempMap.get(o));3 }