HashSet in java

Let’s learn HashSet in java.

HashSet in java

  • Underlying data structure for HashSet is hashtable.
  • HashSet implements Set interface.
  • HashSet does not allow duplicate elements. If any duplicate object is added, then add() method returns “false” without adding any duplicate object.
  • Object are inserted based on their hash code.
  • HashSet does not maintain or preserve insertion order.
  • In HashSet heterogeneous elements are allowed.
  • HashSet allow “null” value.
  • HashSet implements Serializable and Cloneable interface.
  • In HashSet, searching objects is easier. Because objects are stored based on hashcode.
  • HashSet do not implement RandomAccess interface.

HashSet declaration in java

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

Let’s see hashset example in java.

import java.util.HashSet;
import java.util.Iterator;
public class HashSetExample 
   public static void main(String[] args) 
      HashSet<String> hs = new HashSet<>();
      Iterator<String> iterate = hs.iterator();  



Initial capacity : is initial number of buckets that a Hashset object can hold.

Load factor/ fill ratio: is a measure to calculate after how much load a new hashset object is created.

HashSet in java

NOTE: best load factor would be 0.75 with respect to time and space complexity, according to java.

HashSet hierarchy

HashSet in java

Here you can see hierarchy of hashset class. Hashset class extends AbstractSet class. AbstractSet class implements Set interface.

And Set interface extends collection interface. Collection interface extends Iterable interface.

HashSet methods

boolean add(E e)adds the specified element to this set if it is not already present.
void clear()removes all of the elements from this set.
Object clone()returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
boolean contains(Object o)returns true if this set contains the specified element.
boolean isEmpty()method used to check whether the set is empty or not. Returns true if this set contains no elements and false for non-empty condition.
Iterator<E> iterator()returns an iterator over the elements in this set.
boolean remove(Object o)removes the specified element from this set if it is present.
int size()returns the number of elements in this set(its cardinality).

Constructors in hashset

HashSet h = new HashSet();

This constructor creates an empty hashset object with default initial capacity 16 and default fill ratio or load factor 0.75

HashSet h = new HashSet(int initialCapacity);

This constructor creates hashset object with specified initialCapacity with load factor 0.75. Nothing but customized initial capacity and default load factor.

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

Here we can customize load factor.

HashSet h = new HashSet(Collection C);

If you want Interconversion between Collection object then use this constructor.

Iterate hashset in java

To iterate hashset in java we have to use for-each loop.

import java.util.HashSet;
public class HashSetForEach 
   public static void main(String[] args) 
      HashSet<String> hs = new HashSet<String>();
      for(String str : hs)



Reference – docs Oracle, oracle docs