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:
Setis an interface and cannot be instantiated. Therefore, we’ll need one of its implementing classes to create an object likeSortedSet,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 theSetcontains 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 set contains
[1, 8, 5, 3, 0]elements and the collection contains[0, 3, 1]elements. After usingset.removeAll(Collection), theSetwill only remove the collection elements. Now, theSetcontains only[5, 8]elements. -
Suppose the
Setcontains[1, 6, 5, 9, 3, 2]elements and the collection isnull. Here, aNullPointerExceptionis 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
mainclass. -
Line 7: We make a
main()function. -
Line 8: We make a
tryblock. -
Line 10: We declare a
Setof the integer type and name itset1. Here, we instantiate an object of theHashSetclass, as theSetis an interface in Java. -
Lines 11 to 15: We add the elements into
set1using theSet.add()method. -
Line 17: We declare another
Setof the integer type and name itset2. -
Lines 18 to 20: We add the elements into the
set2by using theSet.add()method. -
Line 21: We display the Set,
set1before calling theremoveAll()method. -
Line 23: We display the collection that needs to be removed from
set2. -
Line 26: We call the
removeAll()function to remove the collectionset2fromset1. -
Line 27: We display
set1after calling theremoveAll()method. -
Line 30: We declare a
Setof the integer type and name itset3. We assign it anullvalue. -
Line 32: We display
set2before calling theremoveAll()method. -
Line 34: We display the collection
set3that needs to be removed from theSet. -
Line 37: We call the
removeAll()function to remove the collection from theSet. -
Line 38: We display
set2after calling theremoveAll()method. -
Line 41: We make a
catchblock to encounterNullPointerException. Then, we display the exception when encountered in thetryblock.