<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 `ConcurrentMap`示例 > 原文: [https://javatutorial.net/java-concurrentmap-example](https://javatutorial.net/java-concurrentmap-example) 即使[`Map`](https://docs.oracle.com/javase/7/docs/api/java/util/Map.html) 由許多類實現,但其中許多不是線程安全的,或者其中一些效率不高。 這就是為什么在 Java 1.5 中引入`ConcurrentMap`的原因。 它是線程安全且高效的。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ### 覆蓋的默認實現: * `count` * `replaceAll` * `forEach` * `getOrDefault` * `computerIfAbsent` * `computerIfPresent` `ConcurrentMap`由表示為表存儲桶的節點數組組成,它們在第一次插入后初始化。 ## `ConcurrentMap`和[`HashMap`](https://javatutorial.net/java-hashmap-example)之間的效率比較 * 如果要盡快處理數據,則必須使用所有線程,因此這意味著`ConcurrentMap`在這里更有效。 * 如果只需要單線程訪問,則`HashMap`更快。 * 如果由`HashMap`實現,則添加方法的速度快 3 倍。 * 如果由`ConcurrentMap`實現,則 get 方法會更快。 如果程序需要多個線程訪問,則`ConcurrentMap`是更好的選擇。 但是,如果程序僅使用 1 個線程,則`HashMap`將是更好的選擇。 ![ConcurrentMap](https://img.kancloud.cn/4a/fd/4afda30db3db599e77ce26cccff4f006_667x456.jpg) `ConcurrentMap` ## `ConcurrentMap`中的方法 1. `default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)`:嘗試計算指定鍵及其當前映射值的映射。 2. `default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)`:如果作為參數給出的鍵與值不相關(或為`null`),則嘗試計算其值并輸入 進入此映射,除非為`null`。 3. `default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)`:如果指定鍵的值存在且非空,則嘗試計算新映射,給定鍵及其當前映射值。 4. `default void forEach(BiConsumer<? super K, ? super V> action)`:對當前映射中的每個條目執行給定的操作,直到所有條目都已處理。 5. `default V getOrDefault(Object key, V defaultValue)`:返回指定鍵所映射到的值或如果映射不包含該鍵的映射關系,則返回`defaultValue`(作為第二個參數)。 6. `default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)`:如果鍵尚未與值關聯或與`null`關聯,則它將關聯指定的非`null`值。 7. `V putIfAbsent(K key, V value)`:如果指定的鍵尚未與值關聯,則將其與給定的值關聯。 8. `boolean remove(Object key, Object value)`:僅當當前映射到給定值時,才刪除鍵的條目。 9. `V replace(K key, V value)`:僅當當前映射到某個值時才替換鍵的條目。 10. `boolean replace(K key, V oldValue, V newValue)`:僅當當前映射到給定值時才替換項的條目。 有關 EnumSet 主要方法的更多信息,請隨時訪問原始 [Oracle 文檔](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentMap.html)。 ## 使用上述某些方法的示例程序 ```java import java.util.concurrent.*; class ConcurrentHashMapExample { public static void main(String[] args) { ConcurrentHashMap conCurrHashMap = new ConcurrentHashMap(); conCurrHashMap.put(100, "Elephant"); conCurrHashMap.put(101, "Tiger"); conCurrHashMap.put(102, "Lion"); conCurrHashMap.put(103, "Cow"); // since 103 already exists, this won't work conCurrHashMap.putIfAbsent(103, "Goat"); conCurrHashMap.remove(103, "Goat"); System.out.println("After removal: " + conCurrHashMap); // since 103 was removed, this now works conCurrHashMap.putIfAbsent(103, "Leopard"); System.out.println("After put: " + conCurrHashMap); // changing Goat to Cheetah conCurrHashMap.replace(103, "Leopard", "Cheetah"); System.out.println("Final: " + conCurrHashMap); } } ``` **輸出**: ```java After removal: {100=Elephant, 101=Tiger, 102=Lion, 103=Cow} After put: {100=Elephant, 101=Tiger, 102=Lion, 103=Cow} Final: {100=Elephant, 101=Tiger, 102=Lion, 103=Cow} ```
                  <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>

                              哎呀哎呀视频在线观看