Various operations on HashSet in Java
In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of collection framework.
HashSet in Java
A hash table that is basically a HashMap instance serves as the foundation for the HashSet class, which implements the Set interface. The set's iteration order is not guaranteed; hence the class doesn't really assure that the components will remain in the same order throughout time. The null element is permitted by this class. Additionally, the class provides constant-time performance for operations like as add, delete, contains, and size under the condition that the elements are distributed evenly across the buckets by the hash function. HashSet uses the hashing technique to store the components.
HashSet has quite a several key characteristics, including:
- Carries out Set Interface.
- HashTable is the basic data structure for HashSet.
- Repeated values are not permitted because it supports the Set Interface.
- The sequence in which items are added into a hash set is not always guaranteed to be the same. Depending on their hash code, objects are added.
- In a hash set, NULL elements are permitted.
- Additionally, HashSet supports the Serializable and replicate interfaces.
Different Operations on a HashSet
Appending Elements
The add() method is available for adding new elements to the HashSet. But the HashSet does not keep track of the insertion order. It's important to remember that all duplicate items are ignored and that they are not permitted.
Let us go through a small example program to understand.
File name: Append.java
// Programming in Java to Add Elements to HashSet
//loading the necessary classes
import java.io.*;
import java.util.*;
class Append {
public static void main(String[] args)
{
// building a blank string entity hash set
HashSet<String> h = new HashSet<String>();
// leveraging the add() method to add components
h.add("I");
h.add("am");
h.add("Robin");
h.add("Hood");
// displaying each string entry in the Set
System.out.println("HashSet elements are: " + h);
}
}
Output
HashSet elements are: [I, am, Robin, Hood]
Eliminating Elements
The remove() method can be used to eliminate the values from the HashSet.
Let us go through a small example program to understand.
File name: Eliminate.java
// Java Program Demonstrating the Removal of HashSet Elements and the Importation of //Necessary Classes
import java.io.*;
import java.util.*;
class Eliminate {
public static void main(String[] args)
{
HashSet<String> h = new HashSet<String>();
// adding components to the above-mentioned set
// using the function add()
h.add("I");
h.add("am");
h.add("Robin");
h.add("Hood");
h.add("from");
h.add("Canada");
// printing the components of a hash set
System.out.println("Initial HashSet " + h);
// Eliminating an item
h.remove("I");
// displaying the modified HashSet components
System.out.println("After removing element " + h);
// If a element is absent, the return value is false.
System.out.println("Element ON exists in the Set : "
+ h.remove("on"));
}
}
Output
Initial HashSet [I, am, Robin, Hood, from, Canada]
After removing element [ am, Robin, Hood, from, Canada]
Element ON exists in the Set : false
Going through the HashSet repeatedly
Utilize the iterator() method to cycle over the HashSet's elements. The most well-known method also involves using the improved for loop.
Let us go through a small example program to understand.
File name: Iteration.java
// Java Program to Demonstrate HashSet Iteration Importing Necessary Classes
import java.io.*;
import java.util.*;
class Iteration{
public static void main(String[] args)
{
// making a string HashSet with no entries
HashSet<String> h = new HashSet<String>();
// adding components to the above-mentioned set
// using the function add()
h.add("I");
h.add("am");
h.add("Robin");
h.add("Hood");
h.add("from");
h.add("Canada");
// use iterators to iterate across the HashSet
Iterator itr = h.iterator();
// until there is only one element left in the set, true
while (itr.hasNext())
// elements that have been traversed and printed
System.out.print(itr.next() + ", ");
System.out.println();
// traversal using improved for loop
for (String s : h)
// elements that have been traversed and printed
System.out.print(s + ", ");
System.out.println();
}
}
Output
[I, am, Robin, Hood, from, Canada]
[I, am, Robin, Hood, from, Canada]
Hashtable is the base data structure for HashSet. Therefore, the add, remove, and take a simply look (includes method) operations of the HashSet amortise (average or normal case) time complexity in O(1) time.