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

In this shot, we’ll learn to use the Set.removeAll() method in Java.

The Set.removeAll() method is present in the Set interface inside the java.util package.

Note: Set is an interface and cannot be instantiated. Therefore, we’ll need one of its implementing classes to create an object like SortedSet, TreeSet, NavigableSet, HashSet, etc.

The Set.removeAll() method removes all the elements from the Set specified in the collection.

Note: This method throws the NullPointerException. This happens if the Set 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 set contains [1, 8, 5, 3, 0] elements and the collection contains [0, 3, 1] elements. After using set.removeAll(Collection), the Set will only remove the collection elements. Now, the Set contains only [5, 8] elements.

  • Suppose the Set contains [1, 6, 5, 9, 3, 2] elements and the collection is null. Here, a NullPointerException is thrown.

Parameters

The Set.removeAll() accepts only one parameter:

  • Collection: The collection that needs to be removed from the Set.

Return

The Set.removeAll() method returns true if the Set changes due to the method’s call.

Code

Let’s take a look at the code below:

import java.util.Set;
import java.util.HashSet;
class Main
{
public static void main(String args[])
{
try
{
Set<Integer> set1 = new HashSet<Integer>();
set1.add(1);
set1.add(8);
set1.add(5);
set1.add(3);
set1.add(0);
Set<Integer> set2 = new HashSet<Integer>();
set2.add(8);
set2.add(3);
System.out.println("set1 before calling removeAll(): "
+ set1);
System.out.println("Collection that needs to be removed: "
+ set2);
set1.removeAll(set2);
System.out.println("set1 after calling removeAll(): "
+ set1);
Set<Integer> set3 = null;
System.out.println("set2 before calling removeAll(): "
+ set2);
System.out.println("Collection that needs to be removed: "
+ set3);
set2.removeAll(set3);
System.out.println("set2 after calling removeAll(): "
+ set2);
}
catch(NullPointerException e)
{
System.out.println("Exception thrown : " + e);
}
}
}

Explanation

  • Lines 1 and 2: We import the required packages and classes.

  • Line 5: We make a main class.

  • Line 7: We make a main() function.

  • Line 8: We make a try block.

  • Line 10: We declare a Set of the integer type and name it set1. Here, we instantiate an object of the HashSet class, as the Set is an interface in Java.

  • Lines 11 to 15: We add the elements into set1 using the Set.add() method.

  • Line 17: We declare another Set of the integer type and name it set2.

  • Lines 18 to 20: We add the elements into the set2 by using the Set.add() method.

  • Line 21: We display the Set, set1 before calling the removeAll() method.

  • Line 23: We display the collection that needs to be removed from set2.

  • Line 26: We call the removeAll() function to remove the collection set2 from set1.

  • Line 27: We display set1 after calling the removeAll() method.

  • Line 30: We declare a Set of the integer type and name it set3. We assign it a null value.

  • Line 32: We display set2 before calling the removeAll() method.

  • Line 34: We display the collection set3 that needs to be removed from the Set.

  • Line 37: We call the removeAll() function to remove the collection from the Set.

  • Line 38: We display set2 after calling the removeAll() method.

  • Line 41: We make a catch block to encounter NullPointerException. Then, we display the exception when encountered in the try block.

Free Resources