The removeAll() method of Java AbstractSet class is used to remove from this set all of its elements that are contained in the specified collection.
Syntax:
public boolean removeAll(Collection c)
Parameters: This method takes collection c as a parameter containing elements to be removed from this set.
Returns Value: This method returns true if this set changed as a result of the call.
Exception: The method throws three types of exception:
- UnsupportedOperationException – This is thrown if the operation is not supported by this set.
- ClassCastException – This is thrown when the class of an element of this set is not compatible with the specified collection.
- NullPointerException – This is thrown when this set contains a null element and the specified collection does not permit null elements, or if the collection is null.
Below are the examples to illustrate the removeAll() method.
Program 1:
import java.util.*;
public class GFG1 {
public static void main(String[] args) throws Exception
{
try {
AbstractSet<Integer>
abs_set = new TreeSet<Integer>();
abs_set.add( 1 );
abs_set.add( 2 );
abs_set.add( 3 );
abs_set.add( 4 );
abs_set.add( 5 );
System.out.println( "AbstractSet before "
+ "removeAll() operation : "
+ abs_set);
Collection<Integer>
arrlist2 = new ArrayList<Integer>();
arrlist2.add( 1 );
arrlist2.add( 2 );
arrlist2.add( 3 );
System.out.println( "Collection Elements"
+ " to be removed : "
+ arrlist2);
abs_set.removeAll(arrlist2);
System.out.println( "AbstractSet after "
+ "removeAll() operation : "
+ abs_set);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
AbstractSet before removeAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be removed : [1, 2, 3]
AbstractSet after removeAll() operation : [4, 5]
Example 2: For NullPointerException
import java.util.*;
public class GFG1 {
public static void main(String[] args) throws Exception
{
try {
AbstractSet<Integer>
abs_set = new TreeSet<Integer>();
abs_set.add( 1 );
abs_set.add( 2 );
abs_set.add( 3 );
abs_set.add( 4 );
abs_set.add( 5 );
System.out.println( "AbstractSet before "
+ "removeAll() operation : "
+ abs_set);
Collection<Integer>
arrlist2 = new ArrayList<Integer>();
arrlist2 = null ;
System.out.println( "Collection Elements"
+ " to be removed : "
+ arrlist2);
abs_set.removeAll(arrlist2);
System.out.println( "AbstractSet after "
+ "removeAll() operation : "
+ abs_set);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
}
}
|
Output:
AbstractSet before removeAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be removed : null
Exception thrown : java.lang.NullPointerException
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 Sep, 2019
Like Article
Save Article