Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What is HashSet?
Some features of HashSet
Performance of a HashSet
Constructors for HashSet
Methods of HashSet
Frequently Asked Questions
Last Updated: Mar 27, 2024


Crack Google SDE interview : Essential projects
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM


Set is the child interface of Collection in the Collection Framework. Sets are helpful when we want to represent a collection of objects as one entity, where duplicates are not permitted and insertion order is not preserved. In this lesson, we will learn about HashSets. 

Also read, Duck Number in Java Also see,  Swap Function in Java

What is HashSet?

HashSet is a Java class used to access random elements. Hash tables use hash codes to access their elements.

A hashcode helps identify an element by its unique identity in a hash table.

Duplicate elements are not permitted in hash sets. A unique hashcode is assigned to each element in a hash set.

HashSets check if an object already exists within the Set by using its hashcode value.

There are various bucket locations with multiple elements; each bucket location corresponds to a hash code value, which is the same for all elements. There may be different hash codes, however.

Some features of HashSet

  • The data structure used internally is HashTable.
  • No duplicate values are allowed. In the case of duplicate entries, there will be no compile-time or runtime errors. The add() method will return false.
  • All objects are inserted based on the hash code of all objects, so the insertion order is not preserved.
  • The order of insertion is not preserved.
  • Null insertion is possible.
  • The interface can be serialized and cloned.
  • In the case of frequent operations such as Search, HashSet is the best choice.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Performance of a HashSet

HashSets performs best when two parameters are set - the initial capacity and load factor.

In the worst-case scenario, adding an element to a set will have O(n) time complexity. Thus, the suitable HashSet's capacity must be maintained.

Load Factor: A set may need to be resized above a certain fill level according to the load factor. It means whenever our hash set is filled by 70%, the elements are moved to a new hash table of double the size of the original hash table.

Initial Capacity: By reducing the initial capacity, the complexity of the space decreases, but the frequency of rehashing increases, resulting in increased costs. However, the initial memory consumption increases if the initial capacity is high.

The internal working of HashSets is that Map serves as an internal backend for all Set interface classes. HashedSets internally store objects in HashMaps. We use a key-value pair in HashMap, whereas we use a single value in HashSet.

Constructors for HashSet

There are four constructors for HashSet:

  1. HashSet h=new HashSet(); // creates an empty HashSet object with default initial capacity 16 and Fill ratio of 0.75 also known as Load Factor.
  2. HashSet h=new HashSet(int initialCapacity); // creates an empty HashSet with customized initial capacity.
  3. HashSet h=new HashSet(int initialCapacity, float load factor); //creates an empty HashSet with customized initial capacity and the Load Factor.
  4. HashSet h=new HashSet(Collection c); // creates an equivalent HashSet object from the given Collection named c. means interconversion between Collection objects.


Here is a java program for creating a HashSet by using another collection.

import java.util.*;
public class MyClass {
    public static void main(String args[]) {

        HashSet list = new HashSet < > ();

        HashSet h = new HashSet(list);



[1, 3, Ninja, four, 2022, two]


Here is a simple program to show the properties of HashSet.

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
        HashSet h = new HashSet();

        // storing null

        //Trying to add duplicate value "5"which will return false



[null, 1, 3, 5, two]

Try it on java online compiler.

We can conclude from the example above:

  • The duplicate value "5" will return false when trying to add it.
  • We can see that this order is not maintained.
  • It is also possible to perform a null operation.
  • It is possible to mix heterogeneous objects with 1, 2, 3, and null parameters.

Methods of HashSet



add(element) The Set is enhanced with the specified element if it isn't already there.
clear() The Set is cleaned up by removing everyone's items.
clone() It returns a shallow copy of the HashSet instance, which does not include cloning the elements.
contains(Object) The element specified by element is included in the Set.
isEmpty() If the Set does not contain any elements, it is returned as true.
iterator() If this Set contains any elements, it is returned as true.
remove(Object) If this Set contains any elements, it is removed.
size() If this set contains any elements, it is removed.
spliterator() Splits the elements in a set late-binding and fail-fast splitter.


Check out this problem - Duplicate Subtree In Binary Tree

Frequently Asked Questions

  1. Difference between List and Set.
    → The sequence of elements in a collection is maintained in List, whereas Set does not preserve the order of the elements.
    → The List can have duplicate elements, whereas Set does not allow any duplicate elements. 
  2. When should HashSets and HashMaps be used in Java?
    To maintain the uniqueness of the Collection object, we should prefer HashSet to Hashmap. HashMap is better than HashSet in all other cases because of its superior performance.
  3. How do you determine a good initial capacity?
    High initial capacity is the best option when few or no iterations. For small entries with many iterations, a low initial capacity is better.
  4. Is HashSet is synchronized?
    There is no synchronization of the HashSet. If more than one thread modifies the hash set simultaneously, the hash set will become corrupted. External synchronization is required.


In this blog, we have seen what HashSet is in the Collection Framework and also the methods, features, and performance of HashSet.

Recommended Readings:

We hope that this blog has helped you enhance your knowledge about HashSet and if you would like to learn more, check out our articles on the link. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Next article
Live masterclass