Java HashSet Example

HashSet is a collection of unique elements (objects). The HashSet is available with collection package and extends AbstractSet and implements the set interface. The below diagram shows the HashSet class hierarchy where HashSet is extended from the AbstractSet where AbstractSet implements the set interface and AbstractCollection which is extend from collection framework where collection framework is made up of java interfaces. It does not allow any duplicate elements to be stored.

  • The Hash Set does not guarantee the order of the elements.
  • For storage of collection it uses hash table.
  • HashTable is an array of list used to store the information in a most synchronized way.
  • The advantages of using HashSet is that, it can accept null values and it allows the execution of the basic operation such as add ( ) and remove ( ).

also read:

Hierarchy Diagram of HashSet

Java HashSet Hierarchy Diagram Example

HashSet Features

  • The does not create any duplicate objects.
  • It accepts null values.
  • Elements are added faster to the HashSet.
  • Execution time is constant for all basic operation like add, remove and contains.

HashSet Syntax

Class HashSet<T>

Here T represents the generic type parameter. We can write a HashSet to store a group of strings, as followed:

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

HashSet Constructors

  • HashSet( ): This is a default hash set.
  • HashSet(collection c): It contains the objects in the specified collection c.
  • HashSet(int capacity): It is used to specify the initial capacity of hash set and the default capacity of hash set is 16.
  • HashSet (int capacity, float fill ratio): It initialize capacity and fillRatio.

HashSet Methods

boolean add(object key)It is used to add an element object to the HashSet. It returns true when the element is added to HashSet else it returns false.
boolean remove(object key)It is used to remove an element from the HashSet. It returns true if the element is removed else it returns false.
boolean contains(object key)If the value of objects are present in HashSet then it returns true else it returns false.
boolean isEmpty( )If there is no element present in the HashSet then it returns true else it returns false.
Object clone( )It is used to create a duplicate copy of the object.
int size( )It returns the number of elements present in HashSet.
void clear( )It is used to remove all the elements from the HashSet.

HashSet Example

package myproject;

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

public class HashSet1 {
	public static void main(String args[]) {
		HashSet<String> hs = new HashSet<String>();

		System.out.println("Elements using iterator:");
		Iterator<String> it = hs.iterator();
		while (it.hasNext())
  • In the above example we have used hash set class. We have used the value cricket two times just to see whether it creates any duplicate values in the output or not.
  • HashSet hs = new HashSet(); line creates an HashSet which accepts the string value and creates HashSet object using new operator.
  • hs.add(); line is used to add an elements in the HashSet.
  • System.out.println(“Elements using iterator:”); statement is used for printing the given line in the output which is written inside the bracket.
  • Iterator it = hs.iterator(); statement is uses iterator object to traverse through the elements in the HashSet.
  • it.hasNext() method returns true until it reaches the end of the list.

Java HashSet Example

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!