如何在 Java 中遍歷 Map 的每個元素
    
    
            Mohammad Irfan
    2023年10月12日
    
    Java
    Java Map
    Java Collection
    
- 
          
            在 Java 中如何遍歷 Map元素
- 
          
            在 Java 中使用 for迴圈遍歷Map元素
- 
          
            在 Java 中使用 foreach遍歷Map元素
- 
          
            使用 Java 中的 Entry和Iterator遍歷Map元素
- 
          
            使用 Java 中的 for-each和keySet()遍歷Map元素
- 
          
            在 Java 中使用 while迴圈遍歷Map元素
- 
          
            使用 Java 中的 Stream和forEach對Map元素進行遍歷
- 
          
            在 Java 中使用 forEach和lambda遍歷Map元素
 
本教程介紹瞭如何對 map 的各個元素進行遍歷,並列舉了一些示例程式碼來理解。
在 Java 中如何遍歷 Map 元素
    
Map 是一個介面,用來收集鍵值對形式的資料。Java 提供了幾種遍歷 map 元素的方法,如 for 迴圈、for-each 迴圈、while 迴圈、forEach() 方法等。我們來看看例子。
在 Java 中使用 for 迴圈遍歷 Map 元素
我們使用簡單的 for 迴圈來遍歷 Map 元素。在這裡,在迴圈中使用 iterator() 方法來獲取元素。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    for (Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
         entries.hasNext();) {
      Map.Entry<Integer, String> entry = entries.next();
      System.out.println(entry.getKey() + " : " + entry.getValue());
    }
  }
}
輸出:
100 : Hundred
1000 : Thousand
10 : Ten
在 Java 中使用 foreach 遍歷 Map 元素
我們使用 for-each 迴圈和 entrySet() 方法來遍歷 Map 的每個條目。entrySet() 返回 Map 的元素集合。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    for (Map.Entry<Integer, String> entry : map.entrySet()) {
      System.out.println(entry.getKey() + ":" + entry.getValue());
    }
  }
}
輸出:
100 : Hundred
1000 : Thousand
10 : Ten
使用 Java 中的 Entry 和 Iterator 遍歷 Map 元素
iterator() 方法返回一個 Iterator 來遍歷元素,而 Entry 用於收集 Map 的元素。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
    while (entries.hasNext()) {
      Map.Entry<Integer, String> entry = entries.next();
      System.out.println(entry.getKey() + ":" + entry.getValue());
    }
  }
}
輸出:
100 : Hundred
1000 : Thousand
10 : Ten
使用 Java 中的 for-each 和 keySet() 遍歷 Map 元素
keySet() 方法用於收集 Map 的鍵集,進一步使用 for-each 迴圈進行遍歷。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    for (Integer key : map.keySet()) {
      System.out.println(key + " : " + map.get(key));
    }
  }
}
輸出:
100 : Hundred
1000 : Thousand
10 : Ten
在 Java 中使用 while 迴圈遍歷 Map 元素
在這裡,我們使用 iterator() 方法來獲取鍵的遍歷器,然後使用 while 迴圈來遍歷這些鍵。為了獲得鍵的值,我們使用 get() 方法。
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    Iterator<Integer> itr = map.keySet().iterator();
    while (itr.hasNext()) {
      Integer key = itr.next();
      System.out.println(key + " : " + map.get(key));
    }
  }
}
輸出:
100 : Hundred
1000 : Thousand
10 : Ten
使用 Java 中的 Stream 和 forEach 對 Map 元素進行遍歷
我們可以使用流來遍歷元素。在這裡,我們使用 entrySet() 來收集 Map 元素,並通過流的 forEach() 方法進一步遍歷。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    map.entrySet().stream().forEach(System.out::println);
  }
}
輸出:
100=Hundred
1000=Thousand
10=Ten
在 Java 中使用 forEach 和 lambda 遍歷 Map 元素
我們也可以使用 lambda 表示式來遍歷 Map 元素。這裡,我們在 forEach() 方法中使用了 lambda 表示式。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(10, "Ten");
    map.put(100, "Hundred");
    map.put(1000, "Thousand");
    map.forEach((key, value) -> System.out.println(key + " : " + value));
  }
}
輸出:
100 : Hundred
1000 : Thousand
10 : Ten
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
相關文章 - Java Map
- Java 中的對映過濾
- 在 Java 中將 Stream 元素轉換為對映
- 在 Java 中將列表轉換為 map
- 在 Java 中將 map 值轉換為列表
- 用 Java 建立 map
- 在 Java 中建立有序對映