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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java `Map`接口 > 原文: [https://www.programiz.com/java-programming/map](https://www.programiz.com/java-programming/map) #### 在本教程中,我們將學習 Java `Map`接口及其方法。 Java 集合框架的`Map`接口提供了映射數據結構的功能。 它實現了`Collection`接口。 * * * ## 映射的工作原理 在 Java 中,`Map`的元素存儲在**鍵/值**對中。 **鍵**是與各個**值**相關聯的唯一值。 映射不能包含重復的鍵。 并且,每個鍵都與一個值相關聯。 ![Working of the map interface in Java](https://img.kancloud.cn/6e/b8/6eb8e4b3db79f19a7f636cb5b3c9a82d_688x476.png) 我們可以使用與它們關聯的鍵來訪問和修改值。 在上圖中,我們具有以下值:`Amarica`,`Brazil`和`Spain`。 并且我們有相應的鍵:`us`,`br`和`es`。 現在,我們可以使用它們的對應鍵訪問這些值。 **注意**: `Map`接口維護 3 個不同的集合: * 鍵集 * 值集 * 鍵/值關聯(映射)集。 因此,我們可以分別訪問鍵,值和關聯。 * * * ## 實現`Map`的類 由于`Map`是一個接口,因此我們無法從中創建對象。 為了使用`Map`接口的功能,我們可以使用以下類: * [`HashMap`](/java-programming/hashmap "Java HashMap class") * [`EnumMap`](/java-programming/enummap "Java EnumMap Class") * [`LinkedHashMap`](/java-programming/linkedhashmap "Java LinkedHashMap class") * [`WeakHashMap`](/java-programming/weakhashmap "Java WeakhashMap class") * [`TreeMap`](/java-programming/treemap "Java TreeMap class") 這些類在集合框架中定義,并實現`Map`接口。 ![HashMap, TreeMap, EnumMap, LinkedHashMap and WeakHashMap classes implements the Java Map interface.](https://img.kancloud.cn/10/9a/109a6caede62da004d5254efb3f8d140_1200x417.png) * * * ## 擴展`Map`的接口 `Map`接口還通過以下子接口擴展: * [`SortedMap`](/java-programming/sortedmap "Java SortedMap Interface") * [`NavigableMap`](/java-programming/navigablemap "Java NavigableMap Interface") * [`ConcurrentMap`](/java-programming/concurrentmap "Java ConcurrentMap Interface") ![SortedMap, NavigableMap and ConcurrentMap extends the Java Map interface.](https://img.kancloud.cn/f7/db/f7dbe833ecf60abb494904331648d704_1052x430.png) * * * ## 如何使用映射? 在 Java 中,必須導入`java.util.Map`包才能使用`Map`。 導入包后,將按照以下方法創建映射。 ```java // Map implementation using HashMap Map<Key, Value> numbers = new HashMap<>(); ``` 在上面的代碼中,我們創建了一個名為`number`的`Map`。 我們已經使用`HashMap`類來實現`Map`接口。 這里, * `key` - 用于關聯映射中每個元素(值)的唯一標識符 * `value` - 映射中與按鍵相關聯的元素 * * * ## 映射方法 `Map`接口包含`Collection`接口的所有方法。 這是因為`Collection`是`Map`的超級接口。 除了`Collection`接口中可用的方法外,`Map`接口還包括以下方法: * **`put(K, V)`** - 將鍵`K`和值`V`的關聯插入到映射中。 如果鍵已經存在,則新值將替換舊值。 * **`putAll()`** - 將指定映射中的所有條目插入此映射。 * **`putIfAbsent(K, V)`** - 如果鍵`K`尚未與值`V`關聯,則插入關聯。 * **`get(K)`** - 返回與指定鍵`K`關聯的值。 如果找不到鍵,則返回`null`。 * **`getOrDefault(K, defaultValue)`** - 返回與指定鍵`K`關聯的值。 如果未找到鍵,則返回`defaultValue`。 * **`containsKey(K)`** - 檢查映射中是否存在指定的鍵`K`。 * **`containsValue(V)`** - 檢查指定值`V`是否存在于映射中。 * **`replace(K, V)`** - 用新的指定值`V`替換鍵`K`的值。 * **`replace(K, oldValue, newValue)`** - 僅當鍵`K`時,才將鍵`K`的值替換為新值`newValue`與值`oldValue`關聯。 * **`remove(K)`** - 從鍵`K`表示的映射中刪除條目。 * **`remove(K, V)`** - 從映射中刪除具有與值`V`關聯的鍵`K`的條目。 * **`keySet()`** - 返回映射中存在的所有鍵的集合。 * **`values()`** - 返回映射中存在的所有值的集合。 * **`entrySet()`** - 返回映射中存在的所有鍵/值映射的集合。 * * * ## `Map`接口的實現 **1.`HashMap`實現類** ```java import java.util.Map; import java.util.HashMap; class Main { public static void main(String[] args) { // Creating a map using the HashMap Map<String, Integer> numbers = new HashMap<>(); // Insert elements to the map numbers.put("One", 1); numbers.put("Two", 2); System.out.println("Map: " + numbers); // Access keys of the map System.out.println("Keys: " + numbers.keySet()); // Access values of the map System.out.println("Values: " + numbers.values()); // Access entries of the map System.out.println("Entries: " + numbers.entrySet()); // Remove Elements from the map int value = numbers.remove("Two"); System.out.println("Removed Value: " + value); } } ``` **輸出** ```java Map: {One=1, Two=2} Keys: [One, Two] Values: [1, 2] Entries: [One=1, Two=2] Removed Value: 2 ``` 要了解有關`HashMap`的更多信息,請訪問 [Java HashMap](/java-programming/hashmap "Java HashMap Class") 。 * * * **2.`TreeMap`實現類** ```java import java.util.Map; import java.util.TreeMap; class Main { public static void main(String[] args) { // Creating Map using TreeMap Map<String, Integer> values = new TreeMap<>(); // Insert elements to map values.put("Second", 2); values.put("First", 1); System.out.println("Map using TreeMap: " + values); // Replacing the values values.replace("First", 11); values.replace("Second", 22); System.out.println("New Map: " + values); // Remove elements from the map int removedValue = values.remove("First"); System.out.println("Removed Value: " + removedValue); } } ``` **輸出**: ```java Map using TreeMap: {First=1, Second=2} New Map: {First=11, Second=22} Removed Value: 11 ``` 要了解有關`TreeMap`的更多信息,請訪問 [Java TreeMap](/java-programming/treemap "Java TreeMap Class") 。
                  <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>

                              哎呀哎呀视频在线观看