Table of contents
1.
Introduction
2.
What is a HashSet in Java?
2.1.
Syntax of HashSet
3.
Hashing
4.
Features of Java HashSet
5.
HashSet Capacity and Load Factor
5.1.
Capacity
5.2.
Load Factor
6.
Java HashSet Operations with Examples
6.1.
Example 1
6.2.
Example 2
6.3.
Example 3
7.
Frequently Asked Questions
7.1.
What type of information gets stored in HashSet?
7.2.
What do you mean by the capacity of HashSet?
7.3.
What is Hashing?
7.4.
Why use HashSet?
8.
Conclusion
Last Updated: Nov 10, 2025
Easy

HashSet in Java

Author
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

HashSet in Java is a part of the Java Collections Framework and is used to store unique elements without any specific order. It’s built on a hash table, making it efficient for add, remove, and search operations. If you need a collection that avoids duplicates, HashSet in Java is a smart and practical choice.

What is a HashSet in Java?

Java HashSet is a class that is used to create a collection. In this collection, every item is unique, found in java.util package. Java HashSet uses the property of Abstract class to implement the Set interface. It can be used multiple times to store a unique or duplicate set of existing data.

Syntax of HashSet

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable
You can also try this code with Online Java Compiler
Run Code

Hashing

A hash function is any function that can be used to convert arbitrary-sized data to fixed-size values. Hash values, simply hashes, are returned by a hash function. The values are typically used to index a hash table, which is a fixed-size table. This process is called Hashing.

Features of Java HashSet

  • It stores the elements with the help of a technique called hashing.
  • It stores unique elements and allows null values.
  • Since the elements are inserted at the base of their hashcode, it cannot maintain the insertion order.
  • It is the best approach for search operations because of the constant time complexity.

HashSet Capacity and Load Factor

Capacity

The capacity of a HashSet can be defined as the number of elements it can hold. The default capacity of a HashSet is 16. 

Load Factor

The load factor can be expressed as to how completeness of the HashSet. The default load factor is 0.75. The capacity and load factors give us the experience of memory and performance. 

Load Factor = (Number of elements stored in the table)/(Size of the HashTable)

Java HashSet Operations with Examples

Example 1

import java.util.*; 
public class Hash1{ 
    public static void main(String args[]){
        //Creating HashSet and adding elements 
        HashSet<String> set=new HashSet(); 
        set.add("Hello");
        set.add("This"); 
        set.add("is"); 
        set.add("Vertical"); 
        set.add("blogs");
        Iterator<String> i=set.iterator();
    
        while(i.hasNext())
        {
            System.out.println(i.next());
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Vertical
Hello
blogs
This
is
You can also try this code with Online Java Compiler
Run Code

In this code, we used a HashSet to add some values to the map this code. As you can see it totally depends on HashSet so the order of printing is much different from the order of addition. Also try it on java online compiler.

Example 2

import java.util.*; 
public class HashSet2{ 
    public static void main(String args[]){ 
        //Creating HashSet and adding elements 
        HashSet<String> set=new HashSet<String>();
        set.add("One");
        set.add("Step"); 
        set.add("Closer");
        set.add("To");
        set.add("FAANG"); 
        //Traversing elements 
        Iterator<String> it=set.iterator(); 
        
        while(it.hasNext())
        { 
            System.out.println(it.next()); 
        } 
    } 
}
You can also try this code with Online Java Compiler
Run Code

Output

Closer
One
FAANG
Step
To

In this code, we used a HashSet to add some values to the map this code. As we can see, it totally depends on HashSet so the order of printing is much different from the order of addition. Add function is used to add elements in the map, whereas the hasNext checks are a map contains the next element or not. Similarly, .next prints the elements depending upon HashSet.

Example 3

import java.util.*; 
public class Hash3{
    public static void main(String args[]){
        ArrayList<String> list=new ArrayList<String>();
        list.add("Hello");
        list.add("Young"); 
        list.add("Coder");
        
        HashSet<String> set=new HashSet(list); 
        set.add("Saalim");
        Iterator<String> i=set.iterator();
        while(i.hasNext()) 
        { 
            System.out.println(i.next()); 
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Coder
Young
Hello
Saalim
You can also try this code with Online Java Compiler
Run Code

In this code, we used a HashSet to add some values to the map this code. As we can see, it totally depends on HashSet so the order of printing is much different from the order of addition. Add function is used to add elements in the map, whereas the hasNext checks are map contains the next element or not. Similarly, .next prints the elements depending upon the HashSet.

Frequently Asked Questions

What type of information gets stored in HashSet?


HashSet uses a technique called hashing to store the information. In this technique, a key holds the information that determines a unique value, and that value is called a hash code.
 

What do you mean by the capacity of HashSet?


The term capacity simple refers to the number of elements it can contain. A HashSet's memory is dynamically allocated, which means it can increase its size according to the user's needs.
 

What is Hashing?


A hash function is any function that can be used to convert arbitrary-sized data to fixed-size values. Hash values, or simply hashes, are the values returned by a hash function. The values are typically used to index a hash table, which is a fixed-size table. This process is called Hashing.
 

Why use HashSet?

HashSet is used in Java when you want to store unique elements and don’t care about the order. It’s fast for searching, adding, and removing items because it uses hashing. If you need to avoid duplicates and want quick access, HashSet is a great choice.

Conclusion

In this blog, we have covered HashSet. We have given a brief introduction to Java HashSet along with its syntax. We discussed its features along with the HashCapacity and the loaded factor. After that, we discussed different types of HashSet, along with examples.

Recommended Readings:

Live masterclass