HashSet Class In Java Collection Framework

HashSet(under java.util package) is one of the most widely used Collection class in Java. HashSet underlying data structure is HashMap which is internally based on Hashtable. As HashSet implements Set Interface, so duplicates are not allowed in HashSet. If we are trying to insert duplicates we won’t get any compile time or runtime error, add() method simply return false. Insertion order is not preserved and all objects will be inserted based on hashcode of objects. If we are not using generic implementation of HashSet then Heterogeneous objects are allowed in HashSet.

HashSet class implements Set,Cloneable, Serializable interfaces  but not Random Access.

HashSet is best choice, if our frequent operation is retrieval (search) operation, because insertion of element is done on the basis of some hashcode.

Let see the HashSet class declaration:

For all hashing related data structure like HashSet, LinkedHashSet, HashMap, WeakHashMap, IdentityHashMap and Hashtable, the constructors are common.

HashSet Constructors

 Constructor Description
HashSet() It is used to build an empty hashset with the backing HashMap instance has default initial capacity (16) and load factor (0.75).

HashSet set= new HashSet();

HashSet(int capacity) Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
HashSet(int capacity, float loadFactor) Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.
HashSet(Collection<? extends E > c) Constructs a new set containing the elements in the specified collection.


HashSet set = new HashSet(), creates an empty HashSet with default initial  capacity 16 and default initial fill ratio (load-factor) =0.75

Fill Ratio (Load-factor) : After loading the how much factor, a new HashSet object will be created, that factor is called as fill ration or load factor.

Note: For ArrayList collection, new ArrayList is created only, when ArrayList object reaches at it’s maximum capacity, but for HashSet, it will be create after capacity reached to 75% of its current size.

Apart from the methods inherited from its parent class, HashSet defines following methods

                         HashSet Methods

Methods Description
public boolean add(E e) Adds the specified element to this set if it is not already present.
 public void clear() Removes all of the elements from this set.
 public boolean contains(Object o) Returns true if this set contains the specified element.
 public Object clone() Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
 public boolean isEmpty()  Returns true if this set contains no elements.
 public Iterator<E> iterator() Returns an iterator over the elements in this set.
public E remove(int index) Removes the element at the specified position in this list.
public boolean remove(Object o) Removes the specified element from this set if it is present.
public int size()
Returns the number of elements in this set (its cardinality).
public Spliterator<E> spliterator() Creates a late binding and fail-fast Spliterator over the elements in this set.

                  Non-generic HashSet Vs Generic HashSet

Java collection framework was non-generic before JDK 1.5. Afterward version it is generic now. Java new generic collection allows you to have only one type of object in collection. Now it is type safe so typecasting is not required at run time.

Non-generic HashSet

Generic HashSet

//creating non-generic HashSet

HashSet set=new HashSet();

//creating generic HashSet

HashSet<String> set=new HashSet<String>();

It is not type safe so typecasting is required at run time, If we add objects of different types. HashSet as generic collection, is forced to have only specified type of objects in it, so it is type safe. If you try to add another type of object, it will give compile time error.


How we can get synchronized HashSet object in Java?

Note: By default HashSet object is non synchronized, but we can get synchronized version of HashSet by using Collections Utility class synchronizedSet() static method. Below is the method declaration:

static <T> Set<T> synchronizedSet(<Set<T> set) Returns a synchronized (thread-safe) set backed by the specified set.


Similarly we can get synchronized version of List, Map objects by using following methods of Collections Utility class.

static <T> List<T> synchronizedList(<List<T> list) Returns a synchronized (thread-safe) set backed by the specified list.
static <K,V> Map<K,V> synchronizedMap(<Map<K,V> map) Returns a synchronized (thread-safe) map backed by the specified map.

The following programs illustrates some of methods supported by HashSet

Example 1:

This will produce the following result −


If you see the output, you notice insertion order is not preserved & inserted on basis of some hashcode & traversed according to hashtable bucket search. Also you see duplicated are not allowed.

Example 2:



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.