HashSet class

In this article, we will discuss HashSet class – one of the Set implemented classes in detail

Key points about HashSet:

  • HashSet contains only unique elements
  • It doesn’t maintains insertion-order or
  • say stores in random-order


  • HashSet is the implementation class of Set interface (i.e.; HashSet implements Set)
  • HashSet is backed by a hashtable (actually a HashMap instance) to store element/objects
  • Duplicate element/objects are NOT allowed
  • If duplicate value is added again, then there won’t be any compile-time or runtime errors
  • Simply add(object) method returns false for already containing object inside HashSet
  • At any time, HashSet contains only unique element/objects
  • Insertion-order is NOT maintained
  • While iterating through HashSet, we will get items in random-order (as against insertion-order)
  • Allows NULL insertion but maximum of only one NULL value
  • Without generics, HashSet allows to insert any type of objects;
  • with generics, it is type-bounded (except, if we take Object as type within angle brackets)
  • HashSet is non-synchronized
  • Search operation is faster i.e.; searching any element from HashSet is faster, as it uses hashing to store elements
  • Present in java.util package and extends java.util.AbstractSet implements java.util.Set interface
  • Also, implements java.lang.Cloneable, java.io.Serializable marker interfaces which provides special ability to HashSet (provided by JVM at run time) like,
  • java.lang.Cloneable: to create a duplicate object or to clone an object
  • java.io.Serializable: to transfer objects across network

15-HashSet-interace-in-javaSource: Team BenchResources.Net

HashSet constructors:

HashSet hs = new HashSet();

  • creates an empty HashSet object of size 16
  • with default fill ratio 0.75

HashSet hs = new HashSet(int initialCapacity);

  • creates an empty HashSet object of specified size (or initial capacity)
  • with default fill ratio 0.75

HashSet hs = new HashSet(int initialCapacity, float loadFactor);

  • creates an empty HashSet object of specified size (or initial capacity) and
  • specified fill ratio (for example 0.85)

HashSet hs = new HashSet(Collection c);

  • creates an equivalent HashSet object for the specified collection
  • it is basically used for inter-conversion between collection objects

Fill ratio (or Load factor)

  • Fill ratio is also known as Load factor
  • This factor determines when to increase the size of HashSet automatically
  • For example, for the 1st two constructors the default load factor is 0.75 –> which means after filling 75% of HashSet, new HashSet of bigger size will be created
  • For 3rd constructor, programmer can define load factor while creating HashSet object. If programmer defined it to be 0.95, then after filling 95% of HashSet, size of HashSet will be increased automatically
  • The value of Load factor should be in between 0.0 to 1.0

HashSet examples:


package in.bench.resources.java.collection;

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

public class HashSetAddAndRemove {

	public static void main(String[] args) {

		// creating HashSet object of type String
		HashSet<String> hs = new HashSet<String>();

		// adding elements to HashSet object
		hs.add("Sundar Pichai");
		hs.add("Satya Nadella");  
		hs.add("Shiv Nadar");  
		hs.add("Shantanu Narayen");
		hs.add("Sundar Pichai"); // adding duplicate element
		hs.add("Francisco D’Souza");

		// adding null element to HashSet
		hs.add(null); // 2nd null is added to hs

		// creating Iterator reference
		Iterator<String> ceo = hs.iterator();

		System.out.println("Iterating using Iterator\n");

		// iterating using while loop
		while (ceo.hasNext()){

		System.out.println("\n\nprinting inside"
				+ " square brackets []");


Iterating using Iterator

Satya Nadella
Sundar Pichai
Francisco D’Souza
Shiv Nadar
Shantanu Narayen

printing inside square brackets []
[null, Satya Nadella, Sundar Pichai, Francisco D’Souza, 
Shiv Nadar, Shantanu Narayen]

Note: All methods of HashSet is non-synchronized

How to make HashSet synchronized ?

  • HashSet can be easily converted into synchronized HashSet
  • using utility method synchronizedSet(hs); of java.util.Collections class
    Set set = Collections.synchronizedSet(hs);




Happy Coding !!
Happy Learning !!

LinkedHashSet class
Set interface