<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Java - `HashMap Iterator` > 原文: [https://beginnersbook.com/2014/07/java-hashmap-iterator-example/](https://beginnersbook.com/2014/07/java-hashmap-iterator-example/) #### 示例 在上一個教程中,我們看到了[不同的 2 種迭代`HashMap`](https://beginnersbook.com/2013/12/how-to-loop-hashmap-in-java/)的方法。在這個例子中,我們將看到如何使用[`Iterator`](https://beginnersbook.com/2014/06/java-iterator-with-examples/)迭代[`HashMap`](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/)并顯示鍵和值對。我們在下面的例子中遵循的步驟如下: 1)創建[`HashMap`](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html) 并用鍵值對填充它。 2)通過調用`entrySet()`方法獲取鍵值對集合。 3)獲取入口集的迭代器。 4)使用[`Map.Entry`接口](https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html)的`getKey()`和`getValue()`方法顯示鍵值對。 ```java import java.util.HashMap; import java.util.Set; import java.util.Iterator; import java.util.Map; public class HashMapIteratorExample { public static void main(String[] args) { // Creating a HashMap of int keys and String values HashMap<Integer, String> hashmap = new HashMap<Integer, String>(); // Adding Key and Value pairs to HashMap hashmap.put(11,"Value1"); hashmap.put(22,"Value2"); hashmap.put(33,"Value3"); hashmap.put(44,"Value4"); hashmap.put(55,"Value5"); // Getting a Set of Key-value pairs Set entrySet = hashmap.entrySet(); // Obtaining an iterator for the entry set Iterator it = entrySet.iterator(); // Iterate through HashMap entries(Key-Value pairs) System.out.println("HashMap Key-Value Pairs : "); while(it.hasNext()){ Map.Entry me = (Map.Entry)it.next(); System.out.println("Key is: "+me.getKey() + " & " + " value is: "+me.getValue()); } } } ``` 輸出: ```java HashMap Key-Value Pairs : Key is: 33 & value is: Value3 Key is: 55 & value is: Value5 Key is: 22 & value is: Value2 Key is: 11 & value is: Value1 Key is: 44 & value is: Value4 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看