Performing Various Operations on HashSet
Let’s see how to perform a few frequently used operations on the HashSet.
1. Adding Elements in HashSet
To add an element to the HashSet, we can use the add() method. However, the insertion order is not retained in the HashSet. We need to keep a note that duplicate elements are not allowed and all duplicate elements are ignored.
Example
Java
// Java program to Adding Elements to HashSet // Importing required classes import java.io.*; import java.util.*; // Main class // AddingElementsToHashSet class GFG { // Method 1 // Main driver method public static void main(String[] args) { // Creating an empty HashSet of string entities HashSet<String> hs = new HashSet<String>(); // Adding elements using add() method hs.add( "Geek" ); hs.add( "For" ); hs.add( "Geeks" ); // Printing all string el=ntries inside the Set System.out.println( "HashSet elements : " + hs); } } |
HashSet elements : [Geek, For, Geeks]
2. Removing Elements in HashSet
The values can be removed from the HashSet using the remove() method.
Example
Java
// Java program Illustrating Removal Of Elements of HashSet // Importing required classes import java.io.*; import java.util.*; // Main class // RemoveElementsOfHashSet class GFG { // Main driver method public static void main(String[] args) { // Creating an HashSet<String> hs = new HashSet<String>(); // Adding elements to above Set // using add() method hs.add( "Geek" ); hs.add( "For" ); hs.add( "Geeks" ); hs.add( "A" ); hs.add( "B" ); hs.add( "Z" ); // Printing the elements of HashSet elements System.out.println( "Initial HashSet " + hs); // Removing the element B hs.remove( "B" ); // Printing the updated HashSet elements System.out.println( "After removing element " + hs); // Returns false if the element is not present System.out.println( "Element AC exists in the Set : " + hs.remove( "AC" )); } } |
Initial HashSet [A, B, Geek, For, Geeks, Z] After removing element [A, Geek, For, Geeks, Z] Element AC exists in the Set : false
3. Iterating through the HashSet
Iterate through the elements of HashSet using the iterator() method. Also, the most famous one is to use the enhanced for loop.
Example
Code block
A, B, Geek, For, Geeks, Z, A, B, Geek, For, Geeks, Z,
Time Complexity of HashSet Operations: The underlying data structure for HashSet is hashtable. So amortize (average or usual case) time complexity for add, remove and look-up (contains method) operation of HashSet takes O(1) time.
HashSet in Java
Java HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the hash sets which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The class also offers constant time performance for the basic operations like add, remove, contains, and size assuming the hash function disperses the elements properly among the buckets, which we shall see further in the article.