HashSet Java | Java HashSet With Examples

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
By Tarun Luthra
Free
star5
Enrolled: 1000
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
Tarun Luthra
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

HashSet is a collection in Java that doesn't allow any duplicate values to be stored. All elements in a HashSet are always unique, and elements added to a it are stored in a HashMap internally maintained by it.

Scope of Article

  • This article explains HashSet in Java with examples for creating a HashSet, adding and removing elements, and iterating the elements.
  • This article explains the internal implementation of HashSet and its characteristics.

HashSet in Java

HashSet in Java is a data structure formed by combining HashMap and Set concepts. Before diving deep into it, let's first understand HashMap and Set in Java.

What is HashMap?

HashMap is a collection in Java that stores data in a key-value pair. The key can be used to retrieve the value from the HashMap.

Whenever a key-value pair is added to a HashMap, it computes the key's hash using the internal hash() method. HashMap internally maintains an array where each element of the array is a bucket. The hash() method returns an integer value representing index where it has to be inserted.

In earlier Java versions, each bucket contained a linked list of Map entries. In new Java versions, each bucket contains either a tree structure of entries or a linked list of entries.

what is hashmap

What is a Set?

Set is a collection of elements. A set cannot have duplicate values. All the elements in a set are unique. Eg. {1, 2, 3}

What is HashSet in Java?

HashSet is a collection in Java that belongs to the java.util package. It internally uses HashMap to store elements. It extends the AbstractSet class and implements the Set interface.

What is HashSet in Java

Characteristics of HashSet in Java

  • HashSet internally uses HashMap to store objects.
  • It doesn't contain duplicate elements. All elements in a HashSet are unique.
  • It doesn't maintain the insertion order. When we iterate a HashSet, the elements can be accessed in any order.
  • It allows null values to be stored in it.
  • The default initial capacity is 16 and he load factor is 0.75.
  • It is not synchronized i.e situations where multiple threads access and operate on the HashSet object at the same time must be externally synchronized.

Initial Capacity and Load Factor

Before going deep into HashSet, we must first understand two important terms, initial capacity and load factor.

  • Initial Capacity determines the initial size of the HashSet
  • Load Factor determines the point at which resizing of the HashSet should be performed.

Initial Capacity

HashSet internally uses HashMap, and the initial capacity specifies the initial bucket size of the HashMap. The default initial capacity of HashSet is 16.

Load Factor

The load factor is a threshold that decides when the size of the HashSet has to be increased. The default load factor is 0.75, (i.e.), the size of the internal HashMap will be increased once it is 75% filled.

Different Ways to Create HashSet in Java

HashSet in Java can be created in many ways, including creating a HashSet with default capacity, creating a HashSet with initial capacity, and creating a HashSet with initial capacity and load factor.

HashSet<T>()

Creates an empty HashSet with an initial capacity of 16 and a load factor of 0.75.

HashSet<T>(int initialCapacity)

Creates an empty HashSet with the provided initialCapacity and a load factor of 0.75

HashSet<T>(int initialCapacity, float loadFactor)

Creates an empty HashSet with the provided initialCapacity and loadFactor.

HashSet<T>(Collection collection)

Creates a HashSet containing all the elements of the provided collection.

Example

Let's consider a HashSet created with an initial capacity of 20 and a load factor of 0.75.

Keeping the load factor as 0.75 implies the size of the internal HashMap will be increased for the first time when it has 15 (75% of 20) elements in it.

Operations on HashSet in Java

HashSet provides several operations that are used to add, remove and iterate the elements of it.

Adding Elements

HashSet provides add() and addAll() methods that are used to insert elements to it.

add()

The add() method inserts the given element to the HashSet if it is not present. It returns true if the element is added and false if it is already present in the HashSet.

Syntax:

Example:

Output:

Explanation:

In this example, when we added 1 the second time using hs.add(1), it returned false because 1 is already present in it.

addAll()

The addAll() method inserts all the elements of the given collection to the HashSet. It returns true if the elements of the collection are added successfully, otherwise false. The addAll() method can be used to perform union of two sets.

Union of two sets A and B is the set of all objects that are a member of A , or B , or both.

Syntax:

Example:

Output:

Explanation:

The statement a.addAll(b) performs union of two sets by adding all the elements of b to a. Duplicate values are removed while performing the union operation.

Removing Elements

HashSet provides remove(), removeAll(), and removeIf() methods that are used to remove elements from it.

remove()

The remove() method removes the given element from the HashSet. It returns true if the element is present in it, otherwise false.

Syntax

Example:

Output:

Explanation:

The methods hs.remove(1) and hs.remove(2) returns true because 1 and 2 are present in the HashSet whereas hs.remove(9) returns false because 9 is not present in the HashSet.

removeAll()

The removeAll() method removes all the elements present in the given collection from the HashSet. It returns true if the HashSet is modified, otherwise false. The removeAll() method can be used to find the difference of two sets.

Differnce of two sets A and B is the set of all objects that are a member of A but not B.

Syntax:

Example:

Output:

Explanation:

The statment a.removeAll(b) performs the differnce of two sets by removing all the elements in a which are not in b.

removeIf()

The removeIf() method removes all the HashSet elements that match the given Predicate.

Predicate is a functional interface in Java that accepts a single input and can return a boolean value.

Syntax:

Example:

Output:

Explanation:

In this example, the predicate x -> x % 2 == 0 is used to remove all the even elements from the HashSet.

retainAll

The retainAll() retains all the elements from a HashSet that are not present in the given collection. The retainAll() method can be used to perform intersection of two sets.

Intersection of two sets A and B is the set of all objects that are a member of both A and B.

Syntax:

Example:

Output:

Explanation:

The statment a.retainAll(b) removes all the elements from a that are not in b.

Finding Elements

HashSet provides two method contains() and containsAll() that are used to find if a given element or elements in a given collection is present in a HashSet respectively.

contains

The contains() method checks if a given element is present in the HashSet. It returns true if the element is present, otherwise false.

Syntax:

Example:

Output:

Explanation:

The statement hs.contains(3) returns true because 3 is present in the HashSet whereas hs.contains(9) returns false because 9 is not present in it.

containsAll

The containsAll() method checks if all the elements in the given collection are present in the HashSet. It returns true if all the elements are present, otherwise false. The containsAll() method can be used to check if a set is a subset of another set.

A set B is a subset of another set A if all elements of the set B are elements of the set A.

Syntax:

Example:

Output:

Is [1, 2] subset of [1, 2, 3, 4]: true

Explanation: The statement a.containsAll(b) returns true because b is a subset of a (i.e) all the elements in b are present in a.

Iterating through the HashSet

HashSet provides the iterator() method that can be used to iterate elements of the HashSet. The elements can also be iterated using for and forEach.

Example

Output:

Explanation:

  1. We used an enhanced for loop to iterate and print the elements of the HashSet.
  2. We used the forEach method to traverse and print the values of the HashSet. The forEach accepts a lambda to perform actions on the elements.
  3. HashSet provides an Iterator object via hs.iterator() that can be used to iterate it. The values are accessed via iterator.next().

Other Operations

MethodDescription
isEmpty()Checks if the HashSet is empty
size()Returns the size of the HashSet
clean()Deletes all the elements from the HashSet
clone()Creates a copy of the HashSet

Examples of HashSet in Java

Example 1 - Create HashSet from an Existing Collection

Given a collection that contains integer values, create a HashSet from it.

Output:

Explanation:

In this example, we passed the collection list to the HashSet constructor to create a HashSet with the elements of the collection.

Example 2 - Check if an Item Exists in HashSet

Given a HashSet with initial values and a value, check if the value is present in it.

Output:

Explanation:

In this example, the contains() method checks if the given element is present in the HashSet. It returns true if the element is present, otherwise false.

Example 3 - Remove Duplicate Values

Given a collection that contains integer values, remove all the duplicate values from them.

Output:

Explanation:

In this example, we passed the list to the HashSet constructor to remove all the duplicate values.

Difference Between HashMap and HashSet

HashMapHashSet
It is the implementation of the Map interface in Java.It is the implementation of the Set interface in Java.
It stores elements in the form of key-value pair.It stores only objects. It doesn't maintain any key-value pair.
It allows single null key and multiple null values to be stored in it.It allows a single null value to be stored in it.
It doesn't allow duplicate keys but allows duplicate values to be stored in it.It doesn't allow duplicate values to be stored in it.
It internally uses hashing concept to store elements.It internally uses HashMap to store elements.

Conclusion

  • HashSet is a collection in Java created by putting together the concepts of a HashMap and a Set.
  • It doesn't contain any duplicate values, and it internally uses HashMap to store the elements.
  • It doesn't guarantee insertion order. When a HashSet is iterated, the elements can be accessed in any order.
  • By default, HashSet has an initial capacity of 16 and a load factor of 0.75.
  • Initial capacity denotes the initial size of the HashSet, and load factor indicates how full the it should be when the capacity is increased.
  • It is really useful when we want a data structure that shouldn't contain any duplicate values.

FAQs

1. Why do we use HashSet in Java?

We use HashSet when we want a data structure that cannot contain any duplicate values and the values can be accessed in any order (i.e) the order of retrieval doesn't matter.

2. Is HashSet ordered in Java?

No, HashSet is unordered in Java. When we insert a new element to a HashSet, it doesn't guarantee the insertion order and when we iterate its elements, they can be accessed in any order.

3. How HashSet works internally in Java?

  • HashSet internally uses HashMap to store the elements. When we insert an element to it using the add() method, the element is internally stored in a HashMap.
  • For example, when we add 5 to HashSet via hashSet.add(5), it will be internally stored in HashMap via map.put(5, new Object()). Here, 5 is the key and new Object() is the value of the map entry.

4. Which is faster, HashMap or HashSet?

  • HashSet internally uses HashMap to store elements. So there shouldn't be much difference in performance between HashMap and HashSet.
  • Both computes hashcode to store objects, and calculating the hashcode of a string or an integer is faster than complex objects with many data members.
  • So, the performance of HashMap and HashSet may be affected depending on the objects stored in them.