HashSet Java Collection Framework

Hey there!! Welcome to flower brackets blog. In this post we are going to learn hashset java collection framework.

Introduction: HashSet Java Collection Framework

HashSet Java Collection Framework

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.


Class HashSet<E>



E - type of elements maintained by this set.

Implemented Interfaces

Serializable, Cloneable, Iterable<E>, Collection<E>, Set<E>


JobStateReasons, LinkedHashSet

The class that hashset class belongs to is java.util package. Here letter “E” is nothing but type of elements which we can add it to the hashset.

The type can be string or integer or employee object or user object etc. and hashset which extends abstractset.

Abstractset extends abstractcollection and abstractcollection extends object.

Next you can see some of the implemented interfaces like serializable, cloneable, iterable, collection and set.

Moving on subclasses of hashset are jobstatereasons and linkedhashset.

What is hashset ?

Hashset is a collection. It won’t allow duplicate elements.

It means hashset contains only unique elements and hashset is an unordered collection.

Hashset would maintain insertion order and in hashset we can add null element.

Also Read – Treeset Java Collection Framework

And hashset is not synchronized. Hashset class implements set interface and it is backed up by hash table.

Hashset class offers constant time performance for basic operations like add, remove, contains and size.

It makes no assurance as to the iteration order of the set. That is, there is no guarantee that the order will remain constant over time.

Hashset class 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 ( )returns true if this set contains no elements.
Iterator 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 ).

Below are some of the methods inherited

Methods inherited from class java.util.AbstractSet
removeAll, equals, hashCode

Methods inherited from class java.util.AbstractCollection
retainAll, addAll, containsAll, toArray, toString, toArray

Methods inherited from class java.lang.Object
wait, notifyAll, notify, finalize, getClass, wait, wait

Methods inherited from interface java.util.Set
toArray, retainAll, removeAll, hashCode, equals, containsAll, addAll, toArray

Example: HashSet Java Collection Framework

import java.util.HashSet;

public class HashsetExample
   public static void main(String[] args)
      // here's hashset declaration
      HashSet<String> hs = new HashSet<String>();

      // adding elements

      // now adding duplicate values to hashset

      // now adding null values to hashset

      // finally printing hashset elements
      System.out.println("---------- HashSet Example in java ------------------");
      System.out.println("Displaying hashset elements : " + hs);


HashSet Java Collection Framework


So this is about hashset java collection framework. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Reference – Oracle help centre

You May Also Like