1. 程式人生 > >java 中遍歷Map的幾種方法

java 中遍歷Map的幾種方法

叠代器 ofo popu arr highlight shm int print number

方法分為兩類:

一類是基於map的Entry;map.entrySet();

一類是基於map的key;map.keySet()

而每一類都有兩種遍歷方式:

a.利用叠代器 iterator;

b.利用for-each循環;

代碼舉例如下:

[java] view plaincopy
  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. }
[java] view plain copy
  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. }

java 中遍歷Map的幾種方法