Constructors of HashSet class
To create a HashSet, we need to create an object of the HashSet class. The HashSet class consists of various constructors that allow the possible creation of the HashSet. The following are the constructors available in this class.
1. HashSet()
This constructor is used to build an empty HashSet object in which the default initial capacity is 16 and the default load factor is 0.75. If we wish to create an empty HashSet with the name hs, then, it can be created as:
HashSet<E> hs = new HashSet<E>();
2. HashSet(int initialCapacity)
This constructor is used to build an empty HashSet object in which the initialCapacity is specified at the time of object creation. Here, the default loadFactor remains 0.75.
HashSet<E> hs = new HashSet<E>(int initialCapacity);
3. HashSet(int initialCapacity, float loadFactor)
This constructor is used to build an empty HashSet object in which the initialCapacity and loadFactor are specified at the time of object creation.
HashSet<E> hs = new HashSet<E>(int initialCapacity, float loadFactor);
4. HashSet(Collection)
This constructor is used to build a HashSet object containing all the elements from the given collection. In short, this constructor is used when any conversion is needed from any Collection object to the HashSet object. If we wish to create a HashSet with the name hs, it can be created as:
HashSet<E> hs = new HashSet<E>(Collection C);
Below is the implementation of the above topics:
Java
// Java program to Demonstrate Working // of HashSet Class // Importing required classes import java.util.*; // Main class // HashSetDemo class GFG { // Main driver method public static void main(String[] args) { // Creating an empty HashSet HashSet<String> h = new HashSet<String>(); // Adding elements into HashSet // using add() method h.add( "India" ); h.add( "Australia" ); h.add( "South Africa" ); // Adding duplicate elements h.add( "India" ); // Displaying the HashSet System.out.println(h); System.out.println( "List contains India or not:" + h.contains( "India" )); // Removing items from HashSet // using remove() method h.remove( "Australia" ); System.out.println( "List after removing Australia:" + h); // Display message System.out.println( "Iterating over list:" ); // Iterating over hashSet items Iterator<String> i = h.iterator(); // Holds true till there is single element remaining while (i.hasNext()) // Iterating over elements // using next() method System.out.println(i.next()); } } |
[South Africa, Australia, India] List contains India or not:true List after removing Australia:[South Africa, India] Iterating over list: South Africa India
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.