Java集合之——遍历Map对象的4种方法
方法一 通过Map.entrySet遍历key和value,在for-each循环中使用entries来遍历.推荐,尤其是容量大时
这是最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
1 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 2 3 for (Map.Entry<Integer, Integer> entry : map.entrySet()) { 4 5 System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 6 7 }
方法二、通过Map.keySet遍历key,通过键找值value遍历(效率低),普遍使用,二次取值
1 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 2 3 for (Integer key : map.keySet()) { 4 5 Integer value = map.get(key); 6 7 System.out.println("Key = " + key + ", Value = " + value); 8 9 }
方法三 如果只需要map中的键或者值,你可以通过Map.keySet或Map.values来实现遍历,而不是用entrySet。在for-each循环中遍历keys或values。
1 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 2 3 //遍历map中的键 4 5 for (Integer key : map.keySet()) { 6 7 System.out.println("Key = " + key); 8 9 } 10 11 //遍历map中的值 12 13 for (Integer value : map.values()) { 14 15 System.out.println("Value = " + value); 16 17 }
方法四:通过Map.entrySet使用iterator遍历key和value
1 Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 2 3 Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); 4 5 while (entries.hasNext()) { 6 7 Map.Entry<Integer, Integer> entry = entries.next(); 8 9 System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 10 11 }
=================================我=======是=======分=======割=======线=================================
全部代码:
1 public static void main(String[] args) { 2 init(); 3 traversal(); 4 } 5 6 // HashMap按照哈希算法来存取键对象,有很好的存取性能 7 private static HashMap<String, String> hMap = new HashMap<>(); 8 9 // TreeMap实现了SortedMap接口,能对键对象进行排序。支持自然排序和客户化排序两种方式。 10 private static TreeMap<String, String> tMap = new TreeMap<>(); 11 12 // map的赋值 13 public static void init() { 14 hMap.put("1", "value1"); 15 hMap.put("2", "value2"); 16 hMap.put("3", "value3"); 17 } 18 19 // map的遍历 20 public static void traversal() { 21 // 第一种:普遍使用,二次取值 22 System.out.println("通过Map.keySet遍历key和value:"); 23 for (String key : hMap.keySet()) { 24 System.out.println("key= " + key + " and value= " + hMap.get(key)); 25 } 26 27 // 第二种 28 System.out.println("通过Map.entrySet使用iterator遍历key和value:"); 29 Iterator<Map.Entry<String, String>> it = hMap.entrySet().iterator(); 30 while (it.hasNext()) { 31 Map.Entry<String, String> entry = it.next(); 32 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); 33 } 34 35 // 第三种:推荐,尤其是容量大时 36 System.out.println("通过Map.entrySet遍历key和value"); 37 for (Map.Entry<String, String> entry : hMap.entrySet()) { 38 System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); 39 } 40 41 // 第四种 42 System.out.println("通过Map.values()遍历所有的value,但不能遍历key"); 43 for (String v : hMap.values()) { 44 System.out.println("value= " + v); 45 } 46 }
输出结果:
1 //第一种 2 通过Map.keySet遍历key和value: 3 key= 1 and value= value1 4 key= 2 and value= value2 5 key= 3 and value= value3 6 7 //第二种 8 通过Map.entrySet使用iterator遍历key和value: 9 key= 1 and value= value1 10 key= 2 and value= value2 11 key= 3 and value= value3 12 13 //第三种 14 通过Map.entrySet遍历key和value 15 key= 1 and value= value1 16 key= 2 and value= value2 17 key= 3 and value= value3 18 19 //第四种 20 通过Map.values()遍历所有的value,但不能遍历key 21 value= value1 22 value= value2 23 value= value3
转载自:https://www.cnblogs.com/guweiwei/p/6515273.html
在全栈的道路上,积极向上、成熟稳重、谦虚好学、怀着炽热的心向前方的走得更远。

浙公网安备 33010602011771号