<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 `HashSet`示例 > 原文: [https://javatutorial.net/java-hashset-example](https://javatutorial.net/java-hashset-example) 使用哈希表進行存儲的集合通常由 Java `HashSet`類創建。 顧名思義,`HashSet`實現`Set`接口,并且還使用一個哈希表,該哈希表是`HashMap`實例。`HashSet`中元素的順序是隨機的。 此類允許使用`null`元素。 就復雜度而言,`HashSet`為基本操作(如添加,刪除,包含和大小)提供恒定的時間性能,前提是假定元素已被函數正確分散。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) ## 有關`HashSet`的重要信息 * `HashSet`通過使用稱為**散列**的機制來存儲元素。 * `HashSet`中不能存在重復的元素。 * `HashSet`允許為空值。 * `HashSet`類不同步。 * `HashSet`的順序不由插入順序維護。 元素(在此類中)是根據其哈希碼插入的。 * 就搜索操作而言,由于`HashSet`具有恒定的時間復雜度,因此它是最好的方法。 * `HashSet`的初始默認容量為 16,而負載系數為 0.75。 ## `HashSet`簡單的結構圖 ![hashset in java](https://img.kancloud.cn/94/3b/943b1ad512aba2088ad0c419958ac04c_864x605.jpg) Java 中的`HashSet` 我們放入`HashMap`中的每個對象都首先通過哈希算法發送。 該算法的唯一目的是為傳遞給它的每個對象生成一個稱為**哈希**的唯一編號。 在上圖中,此算法為字符串`Lisa Morgan`生成了數字 3,為`Bob Wiliams`生成了數字 2,為`Jane Smith`生成了數字 1。 以后,這些數字將作為索引存儲在數組中。 每當您要對`HashSet`中的元素執行任何類型的操作時,您都將通過由哈希算法生成的索引來解決它們。 這就是`HashSet`以隨機順序返回元素的原因。 哈希號是`HashSet`知道的唯一順序。 ## `HashSet`中的構造方法 1. `HashSet hashSet = new HashSet();` 2. `HashSet hashSet = new HashSet(int initialCapacity);` 3. `HashSet hashSet = new HashSet(int initialCapacity, float loadFactor);` 4. `HashSet hashSet = new HashSet(Collection c);` 這些構造函數之間的主要區別在于,在 #1 構造函數中,初始容量為 16,默認負載因子為 0.75,但在 #2 中,您實際上可以設置容量。 負載系數的默認值仍為 0.75。 在構造函數 3 中,您可以設置容量和負載系數。 ## `HashSet`類中的方法 1. `boolean add(Object o)`:用于添加作為參數提供的元素,如果不存在,則返回`false`。 2. `void clear()`:用于刪除所有元素。 3. `boolean contains(Object o)`:如果指定的`Object`在`HashSet`中,則返回`true`;否則,返回`false`。 4. `boolean remove(Object o)`:用于從`HashSet`中刪除指定的`Object`(如果存在)。 5. `Iterator iterator()`:用于返回集合中元素上的迭代器。 6. `boolean isEmpty()`:用于檢查`HashSet`是否為空。 如果為空,則返回`true`;否則為`false`。 7. `int size()`:返回集合的大小。 8. `Object clone()`:創建集合的副本。 有關所有方法的文檔,請訪問 [Oracle 官方文檔頁面](https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html)。 ### 使用`add()`在`HashSet`中添加元素 語法:`HashSet.add(Object o);` ```java import java.io.*; import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> animals = new HashSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); // Displaying the HashSet System.out.println("HashSet: " + animals); } } ``` **輸出**: ```java HashSet: [Elephant, Tiger, Lion] ``` ### 使用`clear()`清空`HashSet` 語法:`HashSet.clear();` **輸出**: ```java import java.io.*; import java.util.HashSet; public class HashSetExample{ public static void main(String args[]) { // Creating an empty HashSet HashSet<String> animals = new HashSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); // Displaying the HashSet System.out.println("HashSet: " + animals); // Clearing the hash set animals.clear(); // Displaying the final Set after clearing; System.out.println("The final set: " + animals); } } ``` ```java HashSet: [Elephant, Tiger, Lion] The final set: [] ``` ### 使用`contains()`檢查`HashSet`中是否存在元素 語法:`Hash_Set.contains(Object o)` ```java import java.io.*; import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> animals = new HashSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); // Displaying the HashSet System.out.println("HashSet: " + animals); // Checking for "Lion" in the hash set System.out.println("Does the HashSet contain 'Lion'? " + animals.contains("Lion")); // Checking for "Elephant" in the hash set System.out.println("Does the HashSet contain 'Elephant'? " + animals.contains("Elephant")); // Checking for "Tiger" in the hash set System.out.println("Does the HashSet contain 'Tiger'? " + animals.contains("Tiger")); // Checking for "Chicken" in the hash set System.out.println("Does the HashSet contain 'Chicken'? " + animals.contains("Chicken"));? } } ``` **輸出**: ```java HashSet: [Elephant, Tiger, Lion] Does the Set contain 'Lion'? true Does the Set contain 'Elephant? true Does the Set contain 'Tiger'? true Does the Set contain 'Chicken'? false ``` ### 使用`remove()`從`HashSet`中刪除元素 語法:`HashSet.remove(Object o)` ```java import java.util.*; import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> animals = new HashSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); // Displaying the HashSet System.out.println("HashSet: " + animals); set.remove("Elephant"); set.remove("Lion"); // Displaying the HashSet after removal System.out.println("HashSet after removing elements: " + animals); } } ``` **輸出**: ```java HashSet: [Elephant, Tiger, Lion] HashSet after removing elements: [Tiger] ``` ### `Iterator()`方法 語法:`Iterator iterator = HashSet.iterator()`; ```java import java.util.*; import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> animals = new HashSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); // Displaying the HashSet System.out.println("HashSet: " + animals); // Creating an iterator Iterator iterator = animals.iterator(); // Displaying the values after iterating through the set System.out.println("The iterator values are: "); while (iterator.hasNext()) { System.out.println(iterator.next()); } } } ``` **輸出**: ```java HashSet: [Elephant, Tiger, Lion] The iterator values are: Elephant Tiger Lion ``` ### 使用`isEmpty()`檢查`HashSet`是否為空 語法:`HashSet.isEmpty()`; ```java import java.io.*; import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> animals = new HashSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); // Displaying the HashSet System.out.println("HashSet: " + animals); // Check for the empty set System.out.println("Is the hash set empty: " + animals.isEmpty()); set.clear(); // Checking after we've cleared it out System.out.println("Is the hash set empty: " + animals.isEmpty()); } } ``` **輸出**: ```java HashSet: [Elephant, Tiger, Lion] Is the hash set empty: false Is the hash set empty: true ``` ### 使用`size()`獲取`HashSet`的大小 語法:`HashSet.size()`; ```java import java.util.*; import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> animals = new HashSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); // Displaying the HashSet System.out.println("HashSet: " + animals); // Get the size of the hash set System.out.println("The size of the hash set is: " + animals.size()); } } ``` **輸出**: ```java HashSet: [Elephant, Tiger, Lion] The size of the hash set is: 3 ``` ### 使用`clone()`克隆`HashSet` 語法:`HashSet.clone()` ```java import java.io.*; import java.util.HashSet; public class HashSetExample { public static void main(String args[]) { // Creating an empty HashSet HashSet<String> animals = new HashSet<String>(); animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); System.out.println("HashSet: " + animals); // Creating a new set HashSet clonedSet = new HashSet(); // Cloning the set using clone() method clonedSet = (HashSet)animals.clone(); // Displaying the new hashset; System.out.println("The new set: " + clonedSet); } } ``` **輸出**: ```java HashSet: [Elephant, Tiger, Lion] The new set: [Elephant, Tiger, Lion] ``` ## 如何迭代`HashSet` 有兩種方法可以遍歷`HashSet`: * 使用迭代器 * 不使用迭代器 **1)使用迭代器** ```java import java.util.HashSet; import java.util.Iterator; class IterateHashSetExample{ public static void main(String[] args) { HashSet<String> animals= new HashSet<String>(); //add elements to HashSet animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); Iterator<String> iterator = animals.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } } } ``` 上面的代碼只是將迭代器“附加”到動物散列集上,然后僅打印每一個迭代器,直到沒有更多為止。 另外,此方法將忽略重復項。 如果有重復項,則重復項僅打印一次。 **輸出**: ```java Elephant Tiger Lion ``` **2)不使用迭代器** ```java import java.util.HashSet; import java.util.Set; class IterateHashSetExample{ public static void main(String[] args) { Set<String> animals = new HashSet<String>(); //add elements to HashSet animals.add("Elephant"); animals.add("Tiger"); animals.add("Lion"); for (String animal : animals) { System.out.println(animal); } } } ``` **輸出**: ```java Elephant Tiger Lion ```
                  <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>

                              哎呀哎呀视频在线观看