<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 `ConcurrentHashSet`示例 > 原文: [https://javatutorial.net/java-concurrenthashset-example](https://javatutorial.net/java-concurrenthashset-example) Java 8 最終允許我們(程序員)在 Java 中創建線程安全的[`ConcurrentHashSet`](https://javatutorial.net/java-hashset-example)。 在此之前,這根本是不可能的。 有一些變種試圖簡化上述類的實現,其中之一就是使用帶有虛擬值[`ConcurrentHashMap`](https://javatutorial.net/java-hashmap-example) 。 但是,您可能已經猜到,[`ConcurrentHashMap`](https://javatutorial.net/java-hashmap-example) 的所有即興創作都有其局限性和風險。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) Java 8 允許我們使用`keySet(defaultVal)`和`newKeySet()`方法來返回 Set,這恰好是一個合適的集合。 這樣可以訪問用戶可以使用的許多必要功能:`contains()`,`remove()`等。 **注意**: 這些方法在`ConcurrentHashMap`中可用,而不是在`ConcurrentMap`接口中可用, 意味著必須創建`ConcurrentHashMap`類型的變量,并將其用作引用。 另一種方法是對象的簡單轉換。 Java 并發 API 中包含許多`Collection`類,例如`ArrayList`的`CopyOnArrayList`,`HashMap`的`ConcurrentHashMap`和`HashSet`的`CopyOnWriteArraySet`。 但是,盡管有所有這些示例,但沒有類似`ConcurrentHashSet`的東西。 許多開發人員說,他們可以使用具有相同值的`ConcurrentHashMap`來實現所需的集合,但是這種方法的問題在于,您擁有的是映射而不是集合。 因此,這將導致無法使用偽值對`ConcurrentHashMap`執行設置操作。 簡而言之,它不是`Set`。 還有其他嘗試創建`ConcurrentHashSet`的即興創作,但現在它們都已過去,因為 Java 8 添加了`newKeySet()`,它返回由`ConcurrentHashMap`支持的`Set`。 ### 如何在 Java 8 中創建`ConcurrentHashSet` ```java ConcurrentHashMap<String, Integer> example = new ConcurrentHashMap<>(); Set<String> exampleSet = example.newKeySet(); exampleSet.add("example"); exampleSet.add("example2"); exampleSet.contains("example2"); exampleSet.remove("example"); ``` 在上面的示例中,我們創建了集合,并向其中添加了 2 個元素,檢查它是否包含某個元素并刪除了某個元素。 **注意**: 這是在 Java 中創建線程安全`Set`的唯一方法。 ### Java 程序使用`java.util.concurrent.ConcurrentHashMap`類上添加的新方法創建`ConcurrentHashSet`。 ```java import java.util.Set; import java.util.concurrent.ConcurrentHashMap; public class Example { public static void main(String[] args) throws Exception { ConcurrentHashMap shoesCost = new ConcurrentHashMap<>(); shoesCost.put("Nike", 80); shoesCost.put("Adidas", 40); shoesCost.put("Reebok", 76); Set shoeCostSet = shoesCost.keySet(); shoeCostSet = shoesCost.newKeySet(); System.out.println("before adding element into concurrent set: " + shoeCostSet); shoeCostSet.add("Puma"); System.out.println("after adding element into concurrent set: " + shoeCostSet); shoeCostSet.contains("Adidas"); shoeCostSet.remove("Reebok"); } } ``` **輸出**: ```java before adding an element into the concurrent set: [Nike, Adidas, Reebok] after adding an element into the concurrent set: [Nike, Adidas, Reebok, Puma] ```
                  <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>

                              哎呀哎呀视频在线观看