Table of contents
1.
Introduction
2.
Java HashSet
3.
Hashing
4.
Features
5.
HashSet Capacity and Load Factor
5.1.
Capacity
5.2.
Load Factor
6.
Examples
6.1.
Code 1
6.2.
Code 2
6.3.
Code 3
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

HashSet

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

Introduction

HashSet is used to implement set collection. Therefore, it contains unique data. Likewise, if you try to put an object already there, HashSet will ignore it.

HashSet allows you to increase one object or use the collection to add in bulk. 

Java HashSet

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

Also see,  Swap Function in Java

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

  • 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 on the base of their hashcode, it cannot maintain the insertion order.
  • It is the best approach for search operation 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 withhold. The default capacity of a HashSet is 16. 

Load Factor

The load factor can be expressed as to how complete the HashSet is. 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)

Examples

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

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

Code 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 the elements in the map, whereas the hasNext checks are map contains the next element or not. Similarly .next prints the elements depending upon HashSet.

Frequently Asked Questions

  1. 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.
     
  2. 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 need.
     
  3. 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.

Key Takeaways

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

Recommended Readings:

 
If you want to excel in these topics, please jump to our website Problems on HashSet

Live masterclass