AbstractSet removeAll() Method in Java (original) (raw)
Last Updated : 12 Feb, 2025
The **removeAll() method of the Java **AbstractSet class is part of the Java Collections Framework. This method is used to remove all elements from the current set that are also contained in another collection or set.
**Example 1: This example is used to **remove all elements from the abstract set using the removeAll() method.
Java `
// Java program to demonstrate // removeAll() method for Integer value import java.util.*;
public class Geeks
{
public static void main(String[] args) throws Exception
{
try
{
// Creating object of AbstractSet
AbstractSet as = new TreeSet();
// Populating set
as.add(1);
as.add(2);
as.add(3);
as.add(4);
as.add(5);
System.out.println("AbstractSet before "
+ "removeAll() operation: "
+ as);
// Creating another object of ArrayList<Integer>
Collection<Integer> l = new ArrayList<Integer>();
l.add(1);
l.add(2);
l.add(3);
System.out.println("Collection Elements"
+ " to be removed: "
+ l);
// Removing elements from AbstractSet
// specified in list
// using removeAll() method
as.removeAll(l);
// print arrlist1
System.out.println("AbstractSet after "
+ "removeAll() operation: "
+ as);
}
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]
Syntax
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 changes as a result of the call.
**Exceptions:
- *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:**Thrown when this set has a null element but the collection doesn’t allow nulls, or if the collection is null.
**Example 2: This program shows **how to use removeAll() Method on AbstractSet with Integer Values and handling NullPointerException.
Java `
// Java program to demonstrate // removeAll() method for Integer value import java.util.*;
public class Geeks { public static void main(String[] args) throws Exception { try {
// Creating object of AbstractSet<Integer>
AbstractSet<Integer>
as = new TreeSet<Integer>();
as.add(1);
as.add(2);
as.add(3);
as.add(4);
as.add(5);
System.out.println("AbstractSet before "
+ "removeAll() operation: "
+ as);
// Creating another object of ArrayList<Integer>
Collection<Integer>
l = new ArrayList<Integer>();
l = null;
System.out.println("Collection Elements"
+ " to be removed: "
+ l);
// Removing elements from AbstractSet
// specified in list
// using removeAll() method
as.removeAll(l);
System.out.println("AbstractSet after "
+ "removeAll() operation: "
+ as);
}
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