<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 `TreeMap`示例 > 原文: [https://javatutorial.net/java-treemap-example](https://javatutorial.net/java-treemap-example) `TreeMap`實現了`Map`接口,還實現了`NavigableMap`以及`Abstract`類。 映射是根據其鍵的自然順序或通過提供初始化時間的[比較器](https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html)進行排序的。 就時間復雜度而言,此實現為`containsKey`,`get`,`put`和`remove`操作提供了`log(n)`成本。 請務必注意,`TreeMap`不會同步,因為如果地圖被多個線程訪問,并且如果至少胎面在結構上修改了地圖,則必須在外部進行同步。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ## 有關`TreeMap`的要點 * `TreeMap`實現`Map`接口。 * `TreeMap`不允許使用空鍵。 而是,拋出`NullPointerException`。 雖然,多個空值可以與不同的鍵關聯。 * `TreeMap`是 [Java 集合框架](https://docs.oracle.com/javase/8/docs/technotes/guides/collections/index.html)的成員。 ## `TreeMap`的繼承圖 ![TreeMap inheritance diagram](https://img.kancloud.cn/d4/02/d4025de5620820f2a3e0f379922d66a1_575x377.jpg) `TreeMap`繼承圖 ## `TreeMap`中的構造方法摘要 1. `TreeMap()`:使用其鍵的自然順序構造一個新的空樹映射。 2. `TreeMap(Comparator<? super K> comparator)`:構造一個新的空樹映射,根據給定的比較器排序。 3. `TreeMap(Map<? extends K, ?extends V> m):`:構造一個新的樹映射,該樹映射包含與給定圖相同的映射,并根據其鍵的自然順序進行排序。 4. `TreeMap(SortedMap<K, ? extends V> m)`:構造一個新的樹映射,該樹映射包含與指定排序圖相同的映射并使用相同的排序。 ## `TreeMap`類中的方法 1. `void clear()`:刪除樹形圖中的所有元素。 2. `Object clone()`:返回`TreeMap`實例的淺表副本。 3. `Comprator<? super K> comparator`:返回用于對當前映射中的鍵進行排序的比較器;如果映射使用其鍵的[自然排序](https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html),則返回`null`。 4. `boolean containsKey(Object key)`:如果當前樹映射中存在指定的鍵,則返回`true`。 5. `boolean containsValue(Object value)`:如果當前任意一個地圖鍵都存在指定值,則返回`true`。 6. `V put(K key, V value)`:將指定的值放入指定的鍵。 7. `V remove(Object key)`:從當前映射中刪除指定的鍵。 8. `V replace(K key, V value)`:僅當當前映射到某個值時,才替換指定鍵的條目。 9. `int size()`:獲取當前樹形圖具有的元素數。 10. `Collection<V> values()`:返回當前映射中包含的值的[集合](https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html)視圖。 有關`EnumSet`主要方法的更多信息,請隨時訪問原始 [Oracle 文檔](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html)。 ### 使用`containsKey()`和`containsValue()`檢查當前樹形圖是否包含指定的鍵或值,并使用`put()`填充`TreeMap` ```java import java.util.*; public class TreeMapExample { public static void main(String[] args) { TreeMap<Integer, String> treeMapExample = new TreeMap<Integer, String>(); // assing values to keys treeMapExample.put(5, "java"); treeMapExample.put(15, "tutorial"); treeMapExample.put(20, "dot"); treeMapExample.put(25, "net"); System.out.println("Current stage of the treeMap: " + treeMapExample); // Checking for the key '15' System.out.println("Is key 15 present in the map: " + treeMapExample.containsKey(15); // Checking for the key '5' System.out.println("Is the key 5 present? " + treeMapExample.containsKey(5)); // Checking for value "java" System.out.println("Is the value 'java' present? " + treeMapExample.containsValue("java")); // Checking for value "tutorial" System.out.println("Is the value 'tutorial' present? " + treeMapExample.containsValue("tutorial")); } } ``` **輸出**: ```java Is the key 15 present? true Is the key 5 present? true Is the value 'java' present? true Is the value 'tutorial' present? true ``` ### 使用`remove()`從`TreeMap`中刪除元素 ```java import java.util.*; public class treeMapExample { public static void main(String args[]) { TreeMap<Integer,String> treeMap=new TreeMap<Integer,String>(); // populating the tree map using put() map.put(5,"Joe"); map.put(10,"Mike"); map.put(15,"Antony"); System.out.println("Before remove(): "); // looping through the tree map so we can get each element for(Map.Entry m:map.entrySet()) { // print key and value System.out.println(m.getKey()+" "+m.getValue()); } map.remove(15); System.out.println("After remove(): "); for(Map.Entry m:map.entrySet()) { // print key and value System.out.println(m.getKey()+" "+m.getValue()); } } } ``` **輸出**: ```java Before remove: 5 Joe 10 Mike 15 Antony After Remove 5 Joe 10 Mike ```
                  <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>

                              哎呀哎呀视频在线观看