YouTip LogoYouTip

Java Hashset

[![Image 1: Java Collection Framework](#) Java Collection Framework](#) HashSet is implemented based on HashMap and is a collection that does not allow duplicate elements. HashSet allows null values. HashSet is unordered, meaning it does not record the insertion order. HashSet is not thread-safe. If multiple threads try to modify a HashSet at the same time, the final result is uncertain. You must explicitly synchronize concurrent access to HashSet when accessing it from multiple threads. HashSet implements the Set interface. !(#) The elements in HashSet are actually objects. Some common primitive types can use their wrapper classes. The wrapper class table corresponding to primitive types is as follows: | Primitive Type | Reference Type | | --- | --- | | boolean | Boolean | | byte | Byte | | short | Short | | int | Integer | | long | Long | | float | Float | | double | Double | | char | Character | The HashSet class is in the java.util package and needs to be imported before use. The syntax is as follows: import java.util.HashSet; // Import HashSet class In the following example, we create a HashSet object sites to store string elements: HashSet sites = new HashSet(); ### Add Elements The HashSet class provides many useful methods. You can use the add() method to add elements: ## Example // Import HashSet class import java.util.HashSet; public class TutorialTest { public static void main(String[] args){ HashSet sites =new HashSet(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Zhihu"); sites.add("Tutorial");// Duplicate elements will not be added System.out.println(sites); } } Execute the above code, and the output is as follows: [Google, Tutorial, Zhihu, Taobao] In the above example, Tutorial was added twice, but it will only appear once in the collection, because each element in the collection must be unique. ### Check if Element Exists We can use the contains() method to determine whether an element exists in the collection: ## Example // Import HashSet class import java.util.HashSet; public class TutorialTest { public static void main(String[] args){ HashSet sites =new HashSet(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Zhihu"); sites.add("Tutorial");// Duplicate elements will not be added System.out.println(sites.contains("Taobao")); } } Execute the above code, and the output is as follows: true ### Delete Elements We can use the remove() method to delete elements from the collection: ## Example // Import HashSet class import java.util.HashSet; public class TutorialTest { public static void main(String[] args){ HashSet sites =new HashSet(); sites.add("Google"); sites.add("Tutorial"); sites.add("Taobao"); sites.add("Zhihu"); sites.add("Tutorial");// Duplicate elements will not be added sites.remove("Taobao");// Delete element, returns true if successful, false otherwise System.out.println(sites); } } Execute the above code, and the output is as follows: [Google, Tutorial, Zhihu] To delete all elements in the collection, you can use the clear method: ## Example // Import HashSet class import java.util.HashSet; public
← Java IteratorJava Hashmap β†’