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());
    }
}


Output:

[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.  

Similar Reads

Java HashSet Features

A few important features of HashSet are mentioned below:...

Internal Working of a HashSet

...

Constructors of HashSet class

All the classes of the Set interface are internally backed up by Map. HashSet uses HashMap for storing its object internally. You must be wondering that to enter a value in HashMap we need a key-value pair, but in HashSet, we are passing only one value....

Methods in HashSet

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....

Performing Various Operations on HashSet

...

Performance of HashSet

METHOD DESCRIPTION add(E e) Used to add the specified element if it is not present, if it is present then return false. clear() Used to remove all the elements from the set. contains(Object o) Used to return true if an element is present in a set. remove(Object o)                                                            Used to remove the element if it is present in set. iterator()  Used to return an iterator over the element in the set. isEmpty() Used to check whether the set is empty or not. Returns true for empty and false for a non-empty condition for set. size() Used to return the size of the set. clone()                                                    Used to create a shallow copy of the set....

Methods Used with HashSet

Let’s see how to perform a few frequently used operations on the HashSet....

FAQs in HashSet in Java

...