标签:blog next put 遍历 ext for val system log
1 Map<String, String> map = new HashMap<String, String>(); 2 map.put("1", "value1"); 3 map.put("2", "value2"); 4 map.put("3", "value3"); 5 6 //第一种:普遍使用,二次取值 7 System.out.println("通过Map.keySet遍历key和value:"); 8 for (String key : map.keySet()) { 9 System.out.println("key= "+ key + " and value= " + map.get(key)); 10 } 11 12 //第二种:通过Map.entrySet使用iterator遍历key和value 13 System.out.println("通过Map.entrySet使用iterator遍历key和value:"); 14 Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); 15 while (it.hasNext()) { 16 Map.Entry<String, String> entry = it.next(); 17 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); 18 } 19 20 ****//第三种:推荐,尤其是容量大时 21 System.out.println("通过Map.entrySet遍历key和value"); 22 for (Map.Entry<String, String> entry : map.entrySet()) { 23 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); 24 } 25 26 //第四种:通过Map.values()遍历所有的value,但不能遍历key 27 System.out.println("通过Map.values()遍历所有的value,但不能遍历key"); 28 for (String v : map.values()) { 29 System.out.println("value= " + v); 30 }32 33 34 =====>List 遍历推荐使用Iterator 35 Iterator:迭代器推荐使用 for 循环,性能更好一点 36 List list1 = new ArrayList(); 37 list1.add("a"); 38 list1.add("b"); 39 list1.add("c"); 40 for (Iterator it = list1.iterator();it.hasNext();){ 41 System.out.println(it.next()); 42 }
标签:blog next put 遍历 ext for val system log
原文地址:http://www.cnblogs.com/sjmbug/p/7029259.html