What is the HashSet.removeAll() method in Java?
In this shot, we learn how to use the HashSet.removeAll() method in Java.
The HashSet.removeAll() method is present in the HashSet class inside the java.util package.
The HashSet.removeAll() method is used to remove all the elements from the HashSet which are specified in the collection.
Note: This method throws
NullPointerExceptionif the HashSet contains anullelement and the specified collection does not permitnullelements, or if the specified collection isnull.
Let’s understand with the help of some examples:
-
Suppose a Hashset contains [1, 8, 5, 3, 0] elements and the collection contains [0, 3, 1] elements. After using
Hashset.removeAll(Collection), the Hashset will only remove the elements of the collection. So, now the HashSet contains only [5, 8] elements. -
Suppose the HashSet contains [1, 6, 5, 9, 3, 2] elements and the collection is
null. Here, aNullPointerExceptionis thrown.
Parameters
The HashSet.removeAll() accepts only one parameter:
- Collection: The collection that needs to be removed from the HashSet.
Return
The HashSet.removeAll() method returns true if the HashSet gets changed as a result of the call of the method.
Code
Let’s take a look at the below code:
import java.io.*;import java.util.HashSet;class Main{public static void main(String args[]){try{HashSet<Integer> hash_set1 = new HashSet<Integer>();hash_set1.add(1);hash_set1.add(8);hash_set1.add(5);hash_set1.add(3);hash_set1.add(0);HashSet<Integer> hash_set2 = new HashSet<Integer>();hash_set2.add(8);hash_set2.add(3);System.out.println("hash_set1 before calling removeAll(): "+ hash_set1);System.out.println("Collection that needs to be removed: "+ hash_set2);hash_set1.removeAll(hash_set2);System.out.println("hash_set1 after calling removeAll(): "+ hash_set1);HashSet<Integer> hash_set3 = null;System.out.println("hash_set2 before calling removeAll(): "+ hash_set2);System.out.println("Collection that needs to be removed: "+ hash_set3);hash_set2.removeAll(hash_set3);System.out.println("hash_set2 after calling removeAll(): "+ hash_set2);}catch(NullPointerException e){System.out.println("Exception thrown : " + e);}}}
Explanation
-
In lines 1 and 2, we import the required packages and classes.
-
In line 5, we make a
Mainclass. -
In line 7, we make a
main()function. -
In line 8, we make a
tryblock. -
In line 10, we declare a
HashSetof integer type i.e.,hash_set1. -
From lines 11 to 15, we add the elements into the HashSet by using the
HashSet.add()method. -
In line 17, we declare a HashSet of integer type i.e.,
hash_set2. -
From lines 18 to 20, we add the elements into the HashSet by using the
HashSet.add()method. -
In line 21, we display the HashSet before calling the
removeAll()method. -
In line 23, we display the collection that needs to be removed from the HashSet.
-
In line 26, we call the
removeAll()function to remove the collection from the HashSet. -
In line 27, we display the HashSet after calling the
removeAll()method. -
In line 30, we declare a HashSet of integer type i.e.,
hash_set3and assign it anullvalue. -
In line 32, we display the HashSet before calling the
removeAll()method. -
In line 34, we display the collection that needs to be removed from the HashSet.
-
In line 37, we call the
removeAll()function to remove the collection from the HashSet. -
In line 38, we display the HashSet after calling the
removeAll()method. -
In line 41, we make a
catchblock to encounterNullPointerExceptionand display the exception when encountered in thetryblock.
This is how to use the removeAll() function to remove all the elements in the HashSet, specified in the collection, from the HashSet.