Java HashSet

Last Updated : 12 Jan 2026

Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface.

The important points about Java HashSet class are:

  • HashSet stores the elements by using a mechanism called hashing.
  • HashSet contains unique elements only.
  • HashSet allows null value.
  • HashSet class is non synchronized.
  • HashSet doesn't maintain the insertion order. Here, elements are inserted on the basis of their hashcode.
  • HashSet is the best approach for search operations.
  • The initial default capacity of HashSet is 16, and the load factor is 0.75.

Difference Between List and Set

A list can contain duplicate elements whereas Set contains unique elements only.

Hierarchy of HashSet class

The HashSet class extends AbstractSet class which implements Set interface. The Set interface inherits Collection and Iterable interfaces in hierarchical order.

A HashSet in Java is a collection class that implements the Set interface, which means it stores a collection of unique elements. It does not allow duplicate elements. Internally, a HashSet uses a HashMap to store its elements. Each element in the HashSet is stored as a key in the HashMap, with a dummy value associated with it. HashSet uses the hash code of an element to determine its position in the underlying hash table, which allows for constant-time performance for basic operations like add, remove, and contains, assuming a good hash function.

Java HashSet class hierarchy

One of the key features of a HashSet is its ability to maintain a collection of elements without any specific order. It means that the order in which elements are added to a HashSet is not preserved when iterating over the elements. If we need to maintain the insertion order, we can use a LinkedHashSet, which is another implementation of the Set interface that maintains the insertion order of its elements.

HashSet does not allow null elements, but it can contain a single null value. If we attempt to add a duplicate element to a HashSet, the add method will return false and the HashSet will remain unchanged. HashSet also provides methods for set operations like union, intersection, and difference. However, unlike some other collection classes, HashSet is not synchronized, meaning that it is not thread-safe. If you need a synchronized (thread-safe) version of HashSet, we can use the Collections.synchronizedSet method to wrap your HashSet with a synchronized set.

HashSet Class Declaration

Let's see the declaration for java.util.HashSet class.

Constructors of Java HashSet Class

SNConstructorDescription
1)HashSet()It is used to construct a default HashSet.
2)HashSet(int capacity)It is used to initialize the capacity of the hash set to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet.
3)HashSet(int capacity, float loadFactor)It is used to initialize the capacity of the hash set to the given integer value capacity and the specified load factor.
4)HashSet(Collection<? extends E> c)It is used to initialize the hash set by using the elements of the collection c.

Methods of Java HashSet Class

SNModifier & TypeMethodDescription
1)booleanadd(E e)It is used to add the specified element to this set if it is not already present.
2)voidclear()It is used to remove all of the elements from the set.
3)objectclone()It is used to return a shallow copy of this HashSet instance: the elements themselves are not cloned.
4)booleancontains(Object o)It is used to return true if this set contains the specified element.
5)booleanisEmpty()It is used to return true if this set contains no elements.
6)Iterator<E>iterator()It is used to return an iterator over the elements in this set.
7)booleanremove(Object o)It is used to remove the specified element from this set if it is present.
8)intsize()It is used to return the number of elements in the set.
9)Spliterator<E>spliterator()It is used to create a late-binding and fail-fast Spliterator over the elements in the set.

Java HashSet Example

Let's see a simple example of HashSet. Notice, the elements iterate in an unordered collection.

HashSetExample1.java

Output:

Five
One
Four
Two
Three

Ignoring Duplicate Elements

In this example, we see that HashSet does not allows duplicate elements.

Example

Compile and Run

Output:

       Ajay
       Vijay
       Ravi

Removing Elements

Here, we see different ways to remove an element.

Example

Compile and Run

Output:

An initial list of elements: [Vijay, Ravi, Arun, Sumit]
After invoking remove(object) method: [Vijay, Arun, Sumit]
Updated List: [Vijay, Arun, Gaurav, Sumit, Ajay]
After invoking removeAll() method: [Vijay, Arun, Sumit]
After invoking removeIf() method: [Arun, Sumit]
After invoking clear() method: []

Java HashSet from another Collection

HashSetExample4.java

Output:

Vijay
Ravi
Gaurav
Ajay

Java HashSet Example: Book

Let's see a HashSet example where we are adding books to set and printing all the books.

Example

Compile and Run

Output:

101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6

Let's understand about Java HashSet in Java with the help of an example program.

Example 1:

Filename: HashSetExample.java

Output:

HashSet: [Apple, Orange, Banana]
Iterating over the HashSet using Iterator: Apple Orange Banana
HashSet converted to array: Apple Orange Banana
HashSet of custom objects: [Person{name='Alice', age=30}, Person{name='Bob', age=25}]