java 中遍历Map的几种方法

方法分为两类:

一类是基于map的Entry;map.entrySet();

一类是基于map的key;map.keySet()

而每一类都有两种遍历方式:

a.利用迭代器 iterator;

b.利用for-each循环;

代码举例如下:

[java]
view plain
copy
print
?

  1. package cn.wzb;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.Map.Entry;
  9. //循环遍历map的方法
  10. public class TestMap {
  11. public static void main(String[] args) {
  12. Map<String, Integer> map = new HashMap<String, Integer>();
  13. map.put(“a”, 1);
  14. map.put(“b”, 2);
  15. map.put(“c”, 3);
  16. // JDK1.4中
  17. // 遍历方法一 hashMap.entrySet() 遍历
  18. System.out.println(“方法一”);
  19. Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
  20. while (it.hasNext()) {
  21. Map.Entry<String, Integer> entry = it.next();
  22. String key = entry.getKey();
  23. Integer value = entry.getValue();
  24. System.out.println(“key = “ + key + “; value = “ + value);
  25. }
  26. System.out.println(“”);
  27. // JDK1.5中,应用新特性For-Each循环 // 遍历方法二
  28. System.out.println(“方法二”);
  29. for (Map.Entry<String, Integer> entry : map.entrySet()) {
  30. String key = entry.getKey();
  31. String value = entry.getValue().toString();
  32. System.out.println(“key =” + key + ” value = “ + value);
  33. }
  34. System.out.println(“”);
  35. // 遍历方法三 hashMap.keySet().iterator() 遍历
  36. System.out.println(“方法三”);
  37. for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
  38. String key = i.next();
  39. System.out.println(” key = “ + key + “; value = “ + map.get(key));
  40. }
  41. /*
  42. for (Iterator<Integer> i = map.values().iterator(); i.hasNext(); ) {
  43. Integer value = i.next();
  44. System.out.println(value));// 循环输出value
  45. }
  46. */
  47. /*
  48. for(Integer value : map.values() ) {
  49. System.out.println(“value = ” + value);
  50. }
  51. */
  52. System.out.println(“”);
  53. // 遍历方法四 Hashmap.keySet()遍历
  54. System.out.println(“方法四”);
  55. for (String key : map.keySet()) {
  56. System.out.println(“key = “ + key + “; value = “ + map.get(key));
  57. }
  58. System.out.println();
  59. // java如何遍历Map <String, ArrayList> map = new HashMap <String, ArrayList<Integer> >();
  60. System.out.println(“java 遍历Map <String, ArrayList<Integer> map = new HashMap <String, ArrayList<Integer>>();”);
  61. System.out.println(“方法一:”);
  62. Map<String, List<Integer> > mapList = new HashMap <String, List<Integer> >();
  63. List<Integer> list = new ArrayList<Integer>();
  64. list.add(1);
  65. list.add(2);
  66. list.add(3);
  67. list.add(4);
  68. list.add(5);
  69. mapList.put(“aaa”, list);
  70. mapList.put(“bbb”, list);
  71. Set<String> keys = mapList.keySet();
  72. Iterator<String> iterator = keys.iterator();
  73. while (iterator.hasNext()) {
  74. String key = iterator.next();
  75. System.out.println(key + ” : “);
  76. List<Integer> arrayList = mapList.get(key);
  77. for (Integer i : arrayList) {
  78. System.out.print(i + “, “);
  79. }
  80. System.out.println();
  81. }
  82. System.out.println(“方法二:”);
  83. for (Map.Entry<String, List<Integer> > entry : mapList.entrySet()) {
  84. String key = entry.getKey();
  85. System.out.println(key + ” : “);
  86. List<Integer> values = (List<Integer>) entry.getValue();
  87. for (Integer value : values) {
  88. System.out.print(value + “, “);
  89. }
  90. System.out.println();
  91. }
  92. }
  93. }
package cn.wzb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

//循环遍历map的方法
public class TestMap {
	public static void main(String[] args) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("a", 1);
		map.put("b", 2);
		map.put("c", 3);
		
		// JDK1.4中
		// 遍历方法一 hashMap.entrySet() 遍历
		System.out.println("方法一");
		Iterator<Entry<String, Integer>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Integer> entry =  it.next();
			String key = entry.getKey();
			 Integer value = entry.getValue();
			System.out.println("key = " + key + "; value = " + value);
		}
		
		System.out.println("");
		
		// JDK1.5中,应用新特性For-Each循环	// 遍历方法二
		System.out.println("方法二");
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue().toString();
			System.out.println("key =" + key + " value = " + value);
		}
		System.out.println("");
		
		// 遍历方法三 hashMap.keySet().iterator() 遍历
		System.out.println("方法三");
		for (Iterator<String> i = map.keySet().iterator(); i.hasNext(); ) {
			 String key = i.next();			
			System.out.println(" key = " + key + "; value = " + map.get(key));
		}
		
	   /*
		for (Iterator<Integer> i = map.values().iterator(); i.hasNext(); ) {
			Integer value = i.next();
			System.out.println(value));// 循环输出value
		}
		*/
		/*
		for(Integer value : map.values() ) {
			System.out.println("value = " + value);
		}
		*/
		
		
		System.out.println("");
		// 遍历方法四  Hashmap.keySet()遍历
		System.out.println("方法四");
		for (String key : map.keySet()) {
			System.out.println("key = " + key + "; value = " + map.get(key));
		}
		System.out.println();
		
		// java如何遍历Map <String, ArrayList> map = new HashMap <String, ArrayList<Integer> >();
		System.out.println("java  遍历Map <String, ArrayList<Integer> map = new HashMap <String, ArrayList<Integer>>();");
		System.out.println("方法一:");
		Map<String, List<Integer> > mapList  = new HashMap <String, List<Integer> >();
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		list.add(3);
		list.add(4);
		list.add(5);
		mapList.put("aaa", list);
		mapList.put("bbb", list);
		
		Set<String> keys = mapList.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			System.out.println(key + " : ");
			
			List<Integer> arrayList = mapList.get(key);
			for (Integer i : arrayList) {
				System.out.print(i + ", ");
			}
			System.out.println();
		}
		
		System.out.println("方法二:");
		
		for (Map.Entry<String, List<Integer> > entry : mapList.entrySet()) {
			String key = entry.getKey();
			System.out.println(key + " : ");
			
			List<Integer> values = (List<Integer>) entry.getValue();
			for (Integer value : values) {
				System.out.print(value + ", ");
			}
			System.out.println();
		}
	}
}

    原文作者:sofokiller
    原文地址: https://blog.csdn.net/sofokiller/article/details/8532084
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞