<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 `LinkedHashMap`示例 > 原文: [https://javatutorial.net/java-linkedhashmap-example](https://javatutorial.net/java-linkedhashmap-example) `LinkedHashMap`是哈希表和鏈表的組合,以可預測的迭代順序實現`Map`接口。[`HashMap`](https://javatutorial.net/java-hashmap-example)和`LinkedHashMap`之間的區別在于`LinkedHashMap`維護著一個雙向鏈表,該列表允許來回掃描所有條目。 順序保持不變,這意味著將鍵插入映射的順序。 如果將重新插入到映射中,則順序不會受到影響。`LinkedHashMap`提供所有可選的`Map`操作,還允許空元素。 就時間復雜度而言,就像[`HashMap`](https://javatutorial.net/java-hashmap-example)一樣,它為諸如`add`,`contains`和`remove`之類的基本操作提供了恒定時間的性能。`LinkedHashMap`有兩個影響其性能的參數,它們是初始容量和負載系數。 此類中的迭代不受容量的影響,因此在為初始容量選擇過高的值方面,它比`HashMap`效率更高。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ## 為什么`LinkedHashMap`有用 1. 它將按照輸入到映射中的順序遍歷所有條目。 2. 就像在`HashMap`中一樣,允許使用空值。 3. 使用雙向鏈接的鏈表,使掃描效率更高。 4. 它對添加或訪問項目的順序有額外的了解。 ## 繼承圖 ![Inheritance diagram](https://img.kancloud.cn/09/16/0916c76ae85d317132ac3016ba566708_1006x682.jpg) 繼承圖 ## `LinkedHashMap`中的構造方法摘要 1. `LinkedHashMap()`:構造一個空的插入順序的 LinkedHashMap,其默認初始容量(16)和默認負載因子(0.75)。 2. `LinkedHashMap(int initialCapacity)`:構造一個空的插入順序的`LinkedHashMap`,并將容量設置為指定的 initialCapacity 參數和默認負載因子(0.75)。 3. `LinkedHashMap(int initialCapacity, float loadFactor)`:使用指定的容量和負載因子構造一個空的插入順序的`LinkedHashMap`。 4. `LinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder)`:使用指定的初始容量,負載系數和排序方式構造一個空的`LinkedHashMap`實例。 5. `LinkedHashMap(Map <? extends K, ? extends V> m)`:構造一個插入順序的`LinkedHashMap`實例,該實例具有與指定映射相同的映射。 ## `LinkedHashMap`類中的方法 1. `void clear()`:從此映射中刪除所有映射。 2. `boolean containsValue(Object value)`:如果`LinkedHashMap`包含指定的值,則返回`true`,否則返回`false`。 3. `Set<Map.Entry<K,V>> entrySet()`:返回當前映射中包含的映射的設置視圖。 4. `V get(Object key)`:返回指定鍵所映射到的值;如果該映射不包含該鍵的映射,則返回`null`。 5. `V getOrDefault(Object key, V defaultValue)`:返回指定鍵所映射到的值;如果該映射不包含該鍵的映射,則返回`defaultValue`。 6. `Set<K> keySet`:返回此映射中包含的鍵的設置視圖。 7. `protected boolean removeEldestEntry(Map. Entry<K, V> eldest)`:返回此映射中包含的值的集合視圖。 8. `void put(K key, V value)`:將指定的`Value`與指定的`Key`相關聯。 (它是從 Java 中的`Map`類繼承的) 有關`EnumSet`主要方法的更多信息,請隨時訪問原始 [Oracle 文檔](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html)。 ### 獲取`LinkedHashMap`的大小,檢查其在特定鍵中是否包含某個值,并檢查其是否為空,最后從`LinkedHashMap`中刪除鍵: ```java import java.util.*; public class LinkedHashMapExample { public static void main(String args[]) { LinkedHashMap<String, String> student = new LinkedHashMap<String, String>(); student.put("name", "Joe"); student.put("major", "Computer Science"); student.put("marital status", "Single"); System.out.println(student); // printing the value of the key called name System.out.println("Key 'name's value: " + student.get("name")); // getting the size of the linkedHashMap (size() is inherited from Map) System.out.println("Size of the LinkedHashMap: " + student.size()); // checking whether the map is empty or not System.out.println("Is the map empty: " + student.isEmpty()); // checking whether the linkedHashMap contains the key specified as an argument System.out.println("Does it contain 'marital status'? "+ student.containsKey("marital status")); // deleting/removing an element from the linkedHashMap works by using the //remove method System.out.println("Deleting element 'name': " + student.remove("name")); } } ``` **輸出**: ```java {name=Joe, major=Computer Science, marital status = Single} Key 'name's value: Joe Size of the LinkedHashMap: 3 Is the map empty: false Does it contain 'marital status'? true Deleting element 'name': "Joe" ``` ### 使用`clear()`清除`LinkedHashMap` ```java import java.util.*; public class LinkedHashMapExample { public static void main(String[] args) { LinkedHashMap<String, String> student = new LinkedHashMap<String, String>(); li_hash_map.put("name", "Joe"); li_hash_map.put("major", "Computer Science"); li_hash_map.put("marital status", "Single"); System.out.println("Current stage of linkedHashMap: " + student); // Clearing the linked hash map using clear() li_hash_map.clear(); System.out.println("Stage after the clear() method: " + student); } } ``` **輸出**: ```java Current stage of linkedHashMap: {"name"="Joe", "major"="Computer Science", "marital status ="Single"} Stage after the clear() method: {} ```
                  <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>

                              哎呀哎呀视频在线观看