Java

HashSet in Java

In java, a built-in class named HashSet can be used to create a collection that utilizes the concept of the hash table to store items. HashMap inherits the properties of different classes and interfaces in the form of hierarchy: HashSet inherits “AbstractSet” class which implements the Set interface, and the Set interface implements the Collection Class which further implements the Iterable interface. The HashSet exists in the java.util package.

This write-up provides a thorough overview of HashSet in java and in this regard it covers the following concepts:

So, let’s start!

What is a HashSet

A class that stores only unique items and doesn’t maintain any order to store the items is referred as HashSet in Java.

How to Work with HashSet

The below-given points must be followed in order to work with HashSet

As HashSet belongs to java.util class so, we need to import the HashSet class from java.util package:

import java.util.HashSet;

Next step is object creation, and to do so, we have to follow the following syntax:

HashSet<String> animals = new HashSet<String>();

The above snippet creates a String type object of HashSet, similarly, we can create the object of other data types as well, for this purpose, we have to specify the equivalent wrapper class of all these data types such as Integer for int, Byte for byte, etc.

How to Add Items in HashSet

The HashSet class has various methods to perform various functionalities, the add() method is one of them which can be used to add items in a HashSet.

Example

In the below-given code, we import the HashSet class at the top of our project, and afterward, we create an object of the HashSet class. After object creation, we utilized that object with add() method to add items in the HashSet:

import java.util.HashSet;

public class HashSetExamples {
    public static void main(String[] args) {
    HashSet animals = new HashSet();
    animals.add("Cat");
    animals.add("Dog");
    animals.add("Horse");
    animals.add("Cat");
    System.out.println(animals);
  }
}

In the above snippet we create an object of HashSet class and then we add some items in the HashSet:

From the output, it is clear that the HashSet doesn’t add the duplicate value “cat”.

How to Check the Existence of an item in HashSet

The contains() can be utilized to test the existence of some specific item in HashSet.

Example

The below code snippet tests the existence of two items “Dog” and “Lion”:

System.out.println(animals.contains("Dog"));

System.out.println(animals.contains("Lion"));

If the specified items found in the HashSet then the contains() method will return true:

The output verifies the working of contains method as it returns true for Dog and false for the Lion.

How to Remove Items from HashSet

The remove() method can be used to delete the items from the HashSet:

Example

following will be the code to delete “Dog” from the HashSet:

animals.remove("Dog");

The code and its respective output are shown below::

The output confirms that the remove() method is working appropriately as it succeeds to delete the specified item.

How to Remove All Items of HashSet

The clear() method can be used to remove all items from the HashSet.

animals.clear();

The below snippet shows how does clear() method works:

The empty HashSet authenticates the working of the clear() method.

How to Find the Size of HashSet

The size() can be used to find the size of the HashSet.

Example

The below piece of code shows how to find the size of HashSet.

animals.size();

The complete code and its respective output are is shown in the below-given screenshot:

The output verifies the appropriateness of the size() method.

How to Iterate Through HashSet

We can iterate through a HashSet using an iterator, for-each loop, and for-each method(). We can iterate through the items and values of the HashSet using the keySet() and values() method respectively.

Example

We utilize the for-each loop to iterate through the elements of HashSet:

for (String i : animals) {

System.out.println(i);

}

The complete code and its output is shown in the below-given snippet:

The output shows that for-each loop iterates through each item of the HashSet.

Conclusion

Importing the HashSet Class and then object creation of that class will make it possible to utilize all of its methods to perform various functionalities. The HashSet class provides a variety of different methods to achieve different functionalities e.g. add() and remove() methods can be used in order to add or remove the items of HashSet respectively. Similarly, the contains() method can be used to test the existence of an item in the HashSet and so on. This write-up explains different aspects of the HashSet class. Moreover, some commonly used methods are used to perform different functionalities over the HashSet.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.