Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the HashSet.removeAll() method in Java?

Harsh Jain

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 NullPointerException if the HashSet contains a null element and the specified collection does not permit null elements, or if the specified collection is null.

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, a NullPointerException is 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);
}
}
}
Use hashset.removeAll() method in Java

Explanation

  • In lines 1 and 2, we import the required packages and classes.

  • In line 5, we make a Main class.

  • In line 7, we make a main() function.

  • In line 8, we make a try block.

  • In line 10, we declare a HashSet of 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_set3 and assign it a null value.

  • 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 catch block to encounter NullPointerException and display the exception when encountered in the try block.

This is how to use the removeAll() function to remove all the elements in the HashSet, specified in the collection, from the HashSet.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring