标签:
创建一个MAP的栗子:
1 Map<String, Integer> tempMap = new HashMap<String, Integer>(); 2 tempMap.put("abc", 1); 3 tempMap.put("aoe", 2); 4 tempMap.put("xyz", 3);
方法一:
1 Iterator<Entry<String, Integer>> it = tempMap.entrySet().iterator(); 2 while (it.hasNext()) { 3 Entry<String, Integer> 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.Entry<String, Integer> entry : 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 (Iterator<String> i = 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<Integer> 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 }
标签:
原文地址:http://www.cnblogs.com/SummerinShire/p/5390696.html