Update appNew update is available. Click here to update.

HashSet

Kashish Saxena
Last Updated: Jan 30, 2023
EASY

Introduction

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. 

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.

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 < > ();
        list.add("Ninja");
        list.add("2022");

        HashSet h = new HashSet(list);
        h.add("1");
        h.add("two");
        h.add("3");
        h.add("four");
        System.out.println(h);
    }
}

 

Output:

[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();
        h.add("1");
        h.add("two");
        h.add("3");

        // storing null
        h.add(null);
        h.add("5");

        //Trying to add duplicate value "5"which will return false
        System.out.println(h.add("5"));
        System.out.println(h);
    }
}

 

Output:

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

Method

Description

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.

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.

Conclusion

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

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!

Was this article helpful ?
0 upvotes