Hashset Methods In Java With Example

Hashset is a type of Java Collection that uses Hash table for storing the data, which internally uses a phenomena known as hashing. HashSet do not allow duplicate values, but allow null elements to be stored and also it does not maintains any insertion order.


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' 

5. boolean isEmpty() 

As the name specifies this method checks whether the hashset is empty or not. If it is empty this method will return true, else it will return false, as shown in 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");

//Checking whether hashset is empty

flag= hashSetObject.isEmpty();

System.out.println("Is hash set empty ? "+flag);

//clearing hashset

hashSetObject.clear();

flag= hashSetObject.isEmpty();

System.out.println("Is hash set empty ? "+flag);
}
}

Output:

Is hash set empty ? false

Is hash set empty ? true 

6. Iterator iterator()

This methods returns the iterator, so that we can traverse over the elements of the hashset , 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");

//Creating object of Iterator class

Iterator itr = hashSetObject.iterator();
 
// Printing out hashset using iterator

while (itr.hasNext()){

System.out.println("Value in hash set are: "+itr.next() + " ");
}
}
}

Output:

Values in hash set are: Java

Values in hash set are: Love

Values in hash set are: I

7. boolean remove(Object o) 

This method removes first Occurrence of the element specified in the argument list from the hashset , and returns true if that element is removed, or false if it is not, 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("hash set values :" +hashSetObject);

//calling remove method to remove "java"

hashSetObject.remove("Java");

System.out.println("hash set values after using remove method:" +hashSetObject);
}
}

Output:

hash set values :[Java, Love, I]

hash set values after using remove method:[Love, I]

8. int size()

This method gives the size of the hashset, as shown in the following program

import java.util.*;

public class HashSetDemo{

public static void main(String args[]){

int size;

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

hashSetObject.add("I");

hashSetObject.add("Love");

hashSetObject.add("Java");

size= hashSetObject.size();

System.out.println("Size of hash set is "+size);
}
}

Output:

Size of hash set is 3