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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java 中的`HashSet`類 > 原文: [https://beginnersbook.com/2013/12/hashset-class-in-java-with-example/](https://beginnersbook.com/2013/12/hashset-class-in-java-with-example/) 此類實現`Set`接口,由哈希表(實際上是`HashMap`實例)支持。它不能保證集合的迭代順序;特別是,它不保證訂單會隨著時間的推移保持不變。該類允許`null`元素。此類未同步。但是它可以像這樣明確地同步:`Set s = Collections.synchronizedSet(new HashSet(...));` **關于`HashSet`的注意事項:** 1. `HashSet`不維護任何順序,元素將以任何隨機順序返回。 2. `HashSet`不允許重復。如果您嘗試在 HashSet 中添加重復元素,則舊值將被覆蓋。 3. `HashSet`允許空值,但是如果插入多個空值,它仍然只返回一個空值。 4. `HashSet`是非同步的。 5. 這個類返回的迭代器是快速失敗的,這意味著如果在創建迭代器之后修改了`HashSet`,迭代器會拋出`ConcurrentModificationException`,除了迭代器自己的`remove`方法之外。 ## `HashSet`示例 ```java import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // HashSet declaration HashSet<String> hset = new HashSet<String>(); // Adding elements to the HashSet hset.add("Apple"); hset.add("Mango"); hset.add("Grapes"); hset.add("Orange"); hset.add("Fig"); //Addition of duplicate elements hset.add("Apple"); hset.add("Mango"); //Addition of null values hset.add(null); hset.add(null); //Displaying HashSet elements System.out.println(hset); } } ``` 輸出: ```java [null, Mango, Grapes, Apple, Orange, Fig] ``` 如您所見,輸出中不存在所有重復值,包括重復的空值。 ### `HashSet`教程 * [刪除`HashSet`中的所有元素](https://beginnersbook.com/2014/08/delete-all-the-elements-from-hashset/) * [如何遍歷`HashSet`](https://beginnersbook.com/2014/08/how-to-iterate-over-a-sethashset/) * [將`HashSet`轉換為數組](https://beginnersbook.com/2014/08/converting-a-hashset-to-an-array/) * [將`HashSet`轉換為`TreeSet`](https://beginnersbook.com/2014/08/how-to-convert-a-hashset-to-a-treeset/) * [將`HashSet`轉換為`List`/`ArrayList`](https://beginnersbook.com/2014/08/convert-hashset-to-a-list-arraylist/) * [`HashSet` vs `HashMap`](https://beginnersbook.com/2014/08/hashset-vs-hashmap-java/) * [`HashSet` vs `TreeSet`](https://beginnersbook.com/2014/08/difference-between-hashset-and-treeset/) ### `HashSet`方法: 1. `boolean add(Element e)`:它將元素 e 添加到列表中。 2. `void clear()`:它從列表中刪除所有元素。 3. `Object clone()`:此方法返回`HashSet`的淺表副本。 4. `boolean contains(Object o)`:它檢查指定的`Object o`是否存在于列表中。如果找到該對象,則返回`true`,否則返回`false`。 5. `boolean isEmpty()`:如果`Set`中沒有元素,則返回`true`。 6. `int size()`:它給出了一個`Set`的元素個數。 7. `boolean(Object o)`:它從`Set`中刪除指定的`Object o`。 #### 參考 [`HashSet`文檔](https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html)
                  <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>

                              哎呀哎呀视频在线观看