Open In App

HashSet in Java

Last Updated : 19 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Let’s first start with an example of the creation of a HashSet object in Java:

import java.util.HashSet;    //import HashSet Class

public class Main {
    public static void main(String[] args) {
        // Create a new HashSet to store strings
        HashSet<String> elements = new HashSet<String>();

        // This HashSet can now be used to add elements, remove elements, etc.
    }
}

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.  

Java HashSet Features

A few important features of HashSet are mentioned below:

  • Implements Set Interface.
  • The underlying data structure for HashSet is Hashtable.
  • As it implements the Set Interface, duplicate values are not allowed.
  • Objects that you insert in HashSet are not guaranteed to be inserted in the same order. Objects are inserted based on their hash code.
  • NULL elements are allowed in HashSet.
  • HashSet also implements Serializable and Cloneable interfaces.

Declaration of HashSet

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable

where E is the type of elements stored in a HashSet.

HashSet Java Example

Java
// Java program to illustrate the concept
// of Collection objects storage in a HashSet
import java.io.*;
import java.util.*;

class CollectionObjectStorage {
  
    public static void main(String[] args)
    {
        // Instantiate an object of HashSet
        HashSet<ArrayList> set = new HashSet<>();

        // create ArrayList list1
        ArrayList<Integer> list1 = new ArrayList<>();

        // create ArrayList list2
        ArrayList<Integer> list2 = new ArrayList<>();

        // Add elements using add method
        list1.add(1);
        list1.add(2);
        list2.add(1);
        list2.add(2);
        set.add(list1);
        set.add(list2);

        // print the set size to understand the
        // internal storage of ArrayList in Set
        System.out.println(set.size());
    }
}

Output
1

Before storing an Object, HashSet checks whether there is an existing entry using hashCode() and equals() methods. In the above example, two lists are considered equal if they have the same elements in the same order. When you invoke the hashCode()  method on the two lists, they both would give the same hash since they are equal. 

Note: HashSet does not store duplicate items,  if you give two Objects that are equal then it stores only the first one, here it is list1.

The Hierarchy of HashSet is as follows:

Hierarchy of HashSet

Internal Working of a HashSet

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. 

Storage in HashMap: Actually the value we insert in HashSet acts as a key to the map Object and for its value, java uses a constant variable. So in the key-value pair, all the values will be the same.

Implementation of HashSet in Java doc:

private transient HashMap map;

// Constructor - 1
// All the constructors are internally creating HashMap Object.
public HashSet()
{
    // Creating internally backing HashMap object
    map = new HashMap();
}

// Constructor - 2
public HashSet(int initialCapacity)
{
    // Creating internally backing HashMap object
    map = new HashMap(initialCapacity);
}

// Dummy value to associate with an Object in Map
private static final Object PRESENT = new Object();

If we look at the add() method of the HashSet class: 

public boolean add(E e)
{
   return map.put(e, PRESENT) == null;
}

We can notice that add() method of the HashSet class internally calls the put() method of backing the HashMap object by passing the element you have specified as a key and constant “PRESENT” as its value. remove() method also works in the same manner. It internally calls the remove method of the Map interface. 

public boolean remove(Object o)
{
  return map.remove(o) == PRESENT;
}

HashSet not only stores unique Objects but also a unique Collection of Objects like ArrayList<E>, LinkedList<E>, Vector<E>,..etc.

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

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

Performing Various Operations on HashSet

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

1. Adding Elements in HashSet

To add an element to the HashSet, we can use the add() method. However, the insertion order is not retained in the HashSet.  We need to keep a note that duplicate elements are not allowed and all duplicate elements are ignored.

Example:

Java
// Java program to Adding Elements to HashSet

// Importing required classes
import java.io.*;
import java.util.*;

// Main class
// AddingElementsToHashSet
class GFG {

    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty HashSet of string entities
        HashSet<String> hs = new HashSet<String>();

        // Adding elements using add() method
        hs.add("Geek");
        hs.add("For");
        hs.add("Geeks");

        // Printing all string el=ntries inside the Set
        System.out.println("HashSet elements : " + hs);
    }
}

Output
HashSet elements : [Geek, For, Geeks]

2. Removing Elements in HashSet

The values can be removed from the HashSet using the remove() method.

Example:

Java
// Java program Illustrating Removal Of Elements of HashSet

// Importing required classes
import java.io.*;
import java.util.*;

// Main class
// RemoveElementsOfHashSet
class GFG {

    // Main driver method
    public static void main(String[] args)
    {
        // Creating an
        HashSet<String> hs = new HashSet<String>();

        // Adding elements to above Set
        // using add() method
        hs.add("Geek");
        hs.add("For");
        hs.add("Geeks");
        hs.add("A");
        hs.add("B");
        hs.add("Z");

        // Printing the elements of HashSet elements
        System.out.println("Initial HashSet " + hs);

        // Removing the element B
        hs.remove("B");

        // Printing the updated HashSet elements
        System.out.println("After removing element " + hs);

        // Returns false if the element is not present
        System.out.println("Element AC exists in the Set : "
                           + hs.remove("AC"));
    }
}

Output
Initial HashSet [A, B, Geek, For, Geeks, Z]
After removing element [A, Geek, For, Geeks, Z]
Element AC exists in the Set : false

3. Iterating through the HashSet

Iterate through the elements of HashSet using the iterator() method. Also, the most famous one is to use the enhanced for loop. 

Example:

Java
import java.util.HashSet;
import java.util.Iterator;

public class HashSetIterationExample {
    public static void main(String[] args) {
        // Create a HashSet of Strings
        HashSet<String> hs = new HashSet<>();

        // Add elements to the HashSet
        hs.add("A");
        hs.add("B");
        hs.add("Geeks");
        hs.add("For");
        hs.add("Geeks");
        hs.add("Z");

        // Using iterator() method to iterate over the HashSet
        System.out.print("Using iterator(): ");
        Iterator<String> iterator = hs.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + ", ");
        }

        System.out.println(); // New line for clarity

        // Using enhanced for loop to iterate over the HashSet
        System.out.print("Using enhanced for loop: ");
        for (String element : hs) {
            System.out.print(element + ", ");
        }
    }
}

Output
Using iterator(): A, B, Geeks, For, Z, 
Using enhanced for loop: A, B, Geeks, For, Z, 

Time Complexity of HashSet Operations: The underlying data structure for HashSet is hashtable. So amortize (average or usual case) time complexity for add, remove and look-up (contains method) operation of HashSet takes O(1) time.

Performance of HashSet

HashSet extends Abstract Set<E> class and implements Set<E>, Cloneable, and Serializable interfaces where E is the type of elements maintained by this set. The directly known subclass of HashSet is LinkedHashSet.

Now for the maintenance of constant time performance, iterating over HashSet requires time proportional to the sum of the HashSet instance’s size (the number of elements) plus the “capacity” of the backing HashMap instance (the number of buckets). Thus, it’s very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important. 

  • Initial Capacity: The initial capacity means the number of buckets when the hashtable (HashSet internally uses hashtable data structure) is created. The number of buckets will be automatically increased if the current size gets full. 
     
  • Load Factor: The load factor is a measure of how full the HashSet is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
     
                  Number of stored elements in the table
Load Factor = -----------------------------------------
                        Size of the hash table 

Example: If internal capacity is 16 and the load factor is 0.75 then the number of buckets will automatically get increased when the table has 12 elements in it.

Effect on performance:

Load factor and initial capacity are two main factors that affect the performance of HashSet operations. A load factor of 0.75 provides very effective performance with respect to time and space complexity. If we increase the load factor value more than that then memory overhead will be reduced (because it will decrease internal rebuilding operation) but, it will affect the add and search operation in the hashtable. To reduce the rehashing operation we should choose initial capacity wisely. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operation will ever occur.

Note: The implementation in a HashSet is not synchronized, in the sense that if multiple threads access a hash set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be “wrapped” using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set as shown below:

Set s = Collections.synchronizedSet(new HashSet(…));

Methods Used with HashSet

1. Methods inherited from class java.util.AbstractSet

Method

Description

equals()Used to verify the equality of an Object with a HashSet and compare them. The list returns true only if both HashSet contains the same elements, irrespective of order.
hashcode()                                                                                   Returns the hash code value for this set.
removeAll(collection)This method is used to remove all the elements from the collection which are present in the set. This method returns true if this set changes as a result of the call.

2. Methods inherited from class java.util.AbstractCollection

Method

Description

addAll(collection)                                                

This method is used to append all of the elements from the mentioned collection to the existing set.

The elements are added randomly without following any specific order.

containsAll(collection) 

This method is used to check whether the set contains all the elements present in the given collection or not.

This method returns true if the set contains all the elements and returns false if any of the elements are missing.

retainAll(collection)This method is used to retain all the elements from the set which are mentioned in the given collection. This method returns true if this set changed as a result of the call.
toArray()This method is used to form an array of the same elements as that of the Set.
toString()The toString() method of Java HashSet is used to return a string representation of the elements of the HashSet Collection.

3. Methods declared in interface java.util.Collection

Method

Description

parallelStream()  Returns a possibly parallel Stream with this collection as its source.
removeIf?(Predicate<? super E> filter)                          Removes all of the elements of this collection that satisfy the given predicate.
stream()Returns a sequential Stream with this collection as its source.
toArray?(IntFunction<T[]> generator)Returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

4. Methods declared in interface java.lang.Iterable

Method

Description

 forEach?(Consumer<? super T> action)                        Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

5. Methods declared in interface java.util.Set

Method

Description

addAll?(Collection<? extends E> c)Adds all of the elements in the specified collection to this set if they’re not already present (optional operation).
containsAll?(Collection<?> c)                                     Returns true if this set contains all of the elements of the specified collection.
equals?(Object o)Compares the specified object with this set for equality.
hashCode()Returns the hash code value for this set.
removeAll?(Collection<?> c)Removes from this set all of its elements that are contained in the specified collection (optional operation).
retainAll?(Collection<?> c)Retains only the elements in this set that are contained in the specified collection (optional operation).
toArray()Returns an array containing all of the elements in this set.
toArray?(T[] a)Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

FAQs in HashSet in Java

Q. What is HashSet in Java?

Answer:

HashSet is a type of class, which extends AbstractSet and implements Set interfaces.

Q. Why is HashSet used?

Answer:

HashSet is used for avoiding duplicate data and to find value with the fast method.

Q. Differences between HashSet and HashMap.

Answer:

Basis

HashSet

HashMap

ImplementationHashSet implements a Set interface.HashMap implements a storesMap interface.
DuplicatesHashSet doesn’t allow duplicate values.HashMap stores the key and value pairs and it does not allow duplicate keys. If the key is duplicate then the old key is replaced with the new value.
Number of objects during storing objectsHashSet requires only one object add(Object o).HashMap requires two objects put(K key, V Value) to add an element to the HashMap object.
Dummy valueHashSet internally uses HashMap to add elements. In HashSet, the argument passed in add(Object) method serves as key K. Java internally associates a dummy value for each value passed in add(Object) method.HashMap does not have any concept of dummy value.
Storing or Adding a mechanismHashSet internally uses the HashMap object to store or add the objects.HashMap internally uses hashing to store or add objects
FasterHashSet is slower than HashMap.HashMap is faster than HashSet.
InsertionHashSet uses the add() method for adding or storing data.HashMap uses the put() method for storing data.
ExampleHashSet is a set, e.g. {1, 2, 3, 4, 5, 6, 7}.HashMap is a key -> value pair(key to value) map, e.g. {a -> 1, b -> 2, c -> 2, d -> 1}.

Q. Differences between HashSet and TreeSet in Java.

Answer:

Basis

HashSet

TreeSet

Speed and internal implement the, throw actionFor operations like search, insert, and delete. It takes constant time for these operations on average. HashSet is faster than TreeSet. HashSet is Implemented using a hash table.TreeSet takes O(Log n) for search, insert and delete which is higher than HashSet. But TreeSet keeps sorted data. Also, it supports operations like higher() (Returns least higher element), floor(), ceiling(), etc. These operations are also O(Log n) in TreeSet and not supported in HashSet. TreeSet is implemented using a Self Balancing Binary Search Tree (Red-Black Tree). TreeSet is backed by TreeMap in Java.
Ordering Elements in HashSet are not ordered.TreeSet maintains objects in Sorted order defined by either the Comparable or Comparator method in Java. TreeSet elements are sorted in ascending order by default. It offers several methods to deal with the ordered set like first(), last(), headSet(), tailSet(), etc.
Null ObjectHashSet allows the null object.TreeSet doesn’t allow null Object and throws NullPointerException, Why, is because TreeSet uses compareTo() method to compare keys, and compareTo() will throw java.lang.NullPointerException.
ComparisonHashSet uses the equals() method to compare two objects in the Set and for detecting duplicates.TreeSet uses compareTo() method for the same purpose. If equals() and compareTo() are not consistent, i.e. for two equal objects equals should return true while compareTo() should return zero, then it will break the contract of the Set interface and will allow duplicates in Set implementations like TreeSet



Previous Article
Next Article

Similar Reads

How to Copy or Append HashSet to Another HashSet in Java?
HashSet is used to store distinct values in Java. HashSet stores the elements in random order, so there is no guarantee of the elements' order. The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. We can copy or append a HashSet to another HashSet. There is a couple of ways to copy HashSet or
4 min read
HashSet contains() Method in Java
The Java.util.HashSet.contains() method is used to check whether a specific element is present in the HashSet or not. So basically it is used to check if a Set contains any particular element. Syntax: Hash_Set.contains(Object element) Parameters: The parameter element is of the type of HashSet. This is the element that needs to be tested if it is p
2 min read
HashSet clear() Method in Java
The Java.util.HashSet.clear() method is used to remove all the elements from a HashSet. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing HashSet. Syntax: Hash_Set.clear() Parameters: The method does not take any parameter R
1 min read
Convert HashSet to a ArrayList in Java
ArrayList class is a resizable array, present in java.util package. The difference between an array and an ArrayList in Java, is that the size of an array cannot be modified (i.e. if you want to append/add or remove element(s) to/from an array, you have to create a new array. However, elements can be added/appended or removed from an ArrayList with
4 min read
How to Find the Minimum and Maximum Value from Java HashSet?
HashSet is used to store distinct values in Java. The HashSet does not guarantee the constant order of elements over time, which means when we iterate a HashSet, there is no guarantee that we get the same order of elements as we added in order. HashSet does not provide any built-in method to get the maximum and minimum values. There are a couple of
3 min read
HashSet isEmpty() Method in Java
The Java.util.HashSet.isEmpty() method is used to check if a HashSet is empty or not. It returns True if the HashSet is empty otherwise it returns False. Syntax: Hash_Set.isEmpty() Parameters: This method does not take any parameter Return Value: The function returns True if the set is empty else returns False. Below program illustrate the Java.uti
1 min read
HashSet iterator() Method in Java
The Java.util.HashSet.iterator() method is used to return an iterator of the same elements as the hash set. The elements are returned in random order from what present in the hash set. Syntax: Iterator iterate_value = Hash_Set.iterator(); Parameters: The function does not take any parameter. Return Value: The method iterates over the elements of th
1 min read
HashSet remove() Method in Java
HashSet remove() method is used to remove a particular element from a HashSet. Note that it is only after JDK version 1.2 and ahead, and will throw compilation errors before in version JDK 1 and JDK1.1. Note: This method returns true if the specified element is present in the HashSet otherwise it returns boolean false. Syntax: HashSet.remove(Object
2 min read
HashSet size() Method in Java
The Java.util.HashSet.size() method is used to get the size of the HashSet or the number of elements present in the HashSet. Syntax: Hash_Set.size() Parameters: This method does not takes any parameter. Return Value: The method returns the size or the number of elements present in the HashSet. Below program illustrate the Java.util.HashSet.size() m
1 min read
HashSet add() Method in Java
The Java.util.HashSet.add() method in Java HashSet is used to add a specific element into a HashSet. This method will add the element only if the specified element is not present in the HashSet else the function will return False if the element is already present in the HashSet. Syntax: Hash_Set.add(Object element) Parameters: The parameter element
1 min read