<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 中的`HashMap` > 原文: [https://beginnersbook.com/2013/12/hashmap-in-java-with-example/](https://beginnersbook.com/2013/12/hashmap-in-java-with-example/) `HashMap`是一個基于`Map`的集合類,用于存儲鍵值對,表示為`HashMap<Key, Value>`或`HashMap<K, V>`。此類不保證映射的順序。它類似于`Hashtable`類,除了它是不同步的并且允許空值(空值和空鍵)。 它不是有序集合,這意味著它不會按照它們插入`HashMap`的順序返回鍵和值。它不會對存儲的鍵和值進行排序。您必須導入`java.util.HashMap`或其超類才能使用`HashMap`類和方法。 ## Java 中的`HashMap`示例: 在這個例子中,我們已經演示了幾乎所有`HashMap`類的重要方法。 ```java import java.util.HashMap; import java.util.Map; import java.util.Iterator; import java.util.Set; public class Details { public static void main(String args[]) { /* This is how to declare HashMap */ HashMap<Integer, String> hmap = new HashMap<Integer, String>(); /*Adding elements to HashMap*/ hmap.put(12, "Chaitanya"); hmap.put(2, "Rahul"); hmap.put(7, "Singh"); hmap.put(49, "Ajeet"); hmap.put(3, "Anuj"); /* Display content using Iterator*/ Set set = hmap.entrySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) { Map.Entry mentry = (Map.Entry)iterator.next(); System.out.print("key is: "+ mentry.getKey() + " & Value is: "); System.out.println(mentry.getValue()); } /* Get values based on key*/ String var= hmap.get(2); System.out.println("Value at index 2 is: "+var); /* Remove values based on key*/ hmap.remove(3); System.out.println("Map key and values after removal:"); Set set2 = hmap.entrySet(); Iterator iterator2 = set2.iterator(); while(iterator2.hasNext()) { Map.Entry mentry2 = (Map.Entry)iterator2.next(); System.out.print("Key is: "+mentry2.getKey() + " & Value is: "); System.out.println(mentry2.getValue()); } } } ``` 輸出: ```java key is: 49 & Value is: Ajeet key is: 2 & Value is: Rahul key is: 3 & Value is: Anuj key is: 7 & Value is: Singh key is: 12 & Value is: Chaitanya Value at index 2 is: Rahul Map key and values after removal: Key is: 49 & Value is: Ajeet Key is: 2 & Value is: Rahul Key is: 7 & Value is: Singh Key is: 12 & Value is: Chaitanya ``` ## `HashMap`類方法 以下是`HashMap`類中可用的方法列表。我還在本文末尾介紹了使用這些方法的示例。 1. `void clear()`:它從指定的`Map`中刪除所有鍵和值對。 2. `Object clone()`:它返回映射所有映射的副本,用于將它們克隆到另一個映射中。 3. `boolean containsKey(Object key)`:它是一個布爾函數,它根據是否在映射中找到指定的鍵返回`true`或`false`。 4. `boolean containsValue(Object Value)`:與`containsKey()`方法類似,但它查找指定的值而不是`key`。 5. `Value get(Object key)`:返回指定鍵的值。 6. `boolean isEmpty()`:它檢查映射是否為空。如果映射中沒有鍵值映射,則此函數返回`true`,否則返回`false`。 7. `Set keySet()`:返回從映射中獲取的鍵的`Set`。 8. `Value put(Key k, Value v)`:將鍵值映射插入到映射中。用于上面的例子。 9. `int size()`:返回映射的大小 - 鍵值映射的數量。 10. `Set values()`:返回映射的值集合。 11. `Value remove(Object key)`:刪除指定鍵的鍵值對。用于上面的例子。 12. `void putAll(Map m)`:將映射的所有元素復制到另一個指定的映射。 ## `HashMap`教程 以下是在`HashMap`類上發布的教程列表。學習快樂! #### `HashMap`基礎知識 1. [如何迭代`HashMap`](https://beginnersbook.com/2013/12/how-to-loop-hashmap-in-java/) 2. [按鍵和值排序`HashMap`](https://beginnersbook.com/2013/12/how-to-sort-hashmap-in-java-by-keys-and-values/) 3. [獲取`HashMap`的大小](https://beginnersbook.com/2014/07/java-get-size-of-hashmap-example/) 4. [從`HashMap`中刪除鍵值映射](https://beginnersbook.com/2014/07/java-remove-mapping-from-hashmap-example/) 5. [從`HashMap`中刪除所有映射](https://beginnersbook.com/2014/07/java-remove-all-mappings-from-hashmap-example/) 6. [如何檢查`HashMap`是否為空?](https://beginnersbook.com/2014/08/how-to-check-if-a-hashmap-is-empty-or-not/) #### 獲取/搜索 1. [檢查`HashMap`中是否存在特定鍵](https://beginnersbook.com/2014/07/java-check-if-a-particular-key-exists-in-hashmap-example/) 2. [檢查`HashMap`中是否存在特定值](https://beginnersbook.com/2014/07/java-check-if-a-particular-value-exists-in-hashmap-example/) #### 序列化/同步 1. [序列化`HashMap`](https://beginnersbook.com/2013/12/how-to-serialize-hashmap-in-java/) 2. [同步`HashMap`](https://beginnersbook.com/2013/12/how-to-synchronize-hashmap-in-java-with-example/) #### 差異 1. [`HashMap` 與 `ArrayList`](https://beginnersbook.com/2013/12/difference-between-arraylist-and-hashmap-in-java/) 2. [`HashMap` vs `Hashtable`](https://beginnersbook.com/2014/06/difference-between-hashmap-and-hashtable/) 3. [`HashSet` vs `HashMap`](https://beginnersbook.com/2014/08/hashset-vs-hashmap-java/) #### 其他教程 1. [`HashMap Iterator`示例](https://beginnersbook.com/2014/07/java-hashmap-iterator-example/) 2. [將一個`HashMap`復制到另一個](https://beginnersbook.com/2014/08/how-to-copy-one-hashmap-content-to-another-hashmap/) 3. [使用鍵從`HashMap`獲取值](https://beginnersbook.com/2014/08/hashmap-get-value-from-key-example/) 4. [從`HashMap`獲取鍵集視圖](https://beginnersbook.com/2014/08/java-get-set-view-of-keys-from-hashmap/) 5. [克隆`HashMap`](https://beginnersbook.com/2014/08/clone-a-hashmap-in-java/) #### 參考: [`HashMap`文檔](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html)
                  <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>

                              哎呀哎呀视频在线观看