Hashset Tutorial In Java With Example

Hashset is a type of Java Collection that implements the Set Interface and extends AbstractSet Interface. As HashSet is an implementation of Set Interface, it do not allow duplicate values to be stored, But it can store null elements. Also it does not maintain insertion order.

HashSet uses Hash table for storing the data. Hash table internally uses a phenomena known as hashing, In hashing a unique key is generated automatically called Hash code, which is mapped as the index of the elements in the hashset.


Hierarchy of HashSet class

HashSet Class extends AbstractSet Class and implements Set Interface, Set Interface further extends Collection Interface and Iterable Interface, as shown in figure 1.

Dotted Arrow shows implementation and Bold arrow shows Extending a Class.

Hierarchy of HashSet class


HashSet Methods in JAVA

Let us discuss all the HashSet methods one by one with Examples in Java.

1. boolean add(Object o)

This methods adds the value in the hashSet, only if the element to be added is not present in existing Set, as shown in the following program.

import java.util.*;

public class HashSetDemo{

public static void main(String args[]){

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

//Calling add() mehthod

hashSetObject.add("I");

hashSetObject.add("Love");

hashSetObject.add("Java");

hashSetObject.add("Java");

hashSetObject.add("I");

System.out.println("values in HashSet object "+ hashSetObject);
}
}

Output:

values in HashSet clone object [Java, Love, I]

In Output, We can see that we are not getting repeated values.

2. void clear()

This method as the name suggests remove all the elements from the set, as shown in the following program

import java.util.*;

public class HashSetDemo{

public static void main(String args[]){

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

hashSetObject.add("I");

hashSetObject.add("Love");

hashSetObject.add("Java");

System.out.println("values in HashSet object "+ hashSetObject);

//Calling clear() mehthod

hashSetObject.clear();

System.out.println("values in HashSet object After using Clear method"+ hashSetObject);
}
} 

Output:

values in HashSet object [Java, Love, I]

values in HashSet object After using Clear method[]

3. Object clone()

This method returns the exact same copy of the hashSet object, as shown in the following program

import java.util.*;

public class HashSetDemo{

public static void main(String args[]){

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

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

hashSetObject1.add("I");

hashSetObject1.add("Love");

hashSetObject1.add("Java");

hashSetObject2= (HashSet) hashSetObject1.clone();

System.out.println("values in HashSet clone object "+   hashSetObject2);
}
}

Output:

values in HashSet clone object [Java, I, Love] 

4. boolean contains(Object o)

This method returns true if the calling hashset object contains the specific element as given in the argument list, otherwise it returns false, as shown in the following program.

import java.util.*;

public class HashSetDemo{

public static void main(String args[]){

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

hashSetObject.add("I");

hashSetObject.add("Love");

hashSetObject.add("Java");

if(hashSetObject.contains("I")){

System.out.println("Hash Set Contains 'I'");

}

else{

System.out.println("Hash Set Do not Contains 'I'");

}

if(hashSetObject.contains("India")){

System.out.println("Hash Set Contains 'India'");

}

else{

System.out.println("Hash Set Do not Contains 'India'");
}
}
}

Output:

Hash Set Contains 'I'

Hash Set Do not Contains 'India'

Read All HashSet Methods With Example in Java


 Example of HashSet class

Let us discuss Hash set with the help of program; following program has been divided into 3 Steps that we will discuss one by one.

import java.util.*;

public class HashSetDemo{

public static void main(String args[]){
//Step: 1
HashSet <String> hashSetObject= new HashSet <String>();

//Step: 2

hashSetObject.add("I"); 

hashSetObject.add("Love");

hashSetObject.add("Java");

hashSetObject.add("Java");

hashSetObject.add("I");

//Step: 3

System.out.println("Values in HashSet are:" +hashSetObject); ) 
}
} 
  • In Step 1, we have created an object of HashSet collection, we have defined element to be stored of String type.
  • In Step 2, we have used add method to add elements of String type in the hashSet object that we have created in Step 1.
  • In Step 3, we have printed all the values of hashSet object.

Output:

Values in HashSet are:[Java, Love, I]

As we can see in above output Duplicate values are not Stored and Insertion order is not maintained.


HashSet Important Points

  • HashSet only stores unique values, that is duplicate values are not allowed.
  • HashSet do not maintain insertion order, which means it does not return the elements in the order in which they are added.
  • It does not do any kind of sorting to the stored values.
  • HashSet allows only one null value in it, As duplicates are not allowed.
  • HashSet uses hash table to store the values.

Ask Your Question Or Leave A Reply

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