AbstractSet removeAll() Method in Java with Examples
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:
// Java program to demonstrate // removeAll() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] args) throws Exception { try { // Creating object of AbstractSet<Integer> AbstractSet<Integer> abs_set = new TreeSet<Integer>(); // Populating abs_set abs_set.add(1); abs_set.add(2); abs_set.add(3); abs_set.add(4); abs_set.add(5); // print abs_set System.out.println("AbstractSet before " + "removeAll() operation : " + abs_set); // Creating another object of ArrayList<Integer> Collection<Integer> arrlist2 = new ArrayList<Integer>(); arrlist2.add(1); arrlist2.add(2); arrlist2.add(3); // print arrlist2 System.out.println("Collection Elements" + " to be removed : " + arrlist2); // Removing elements from AbstractSet // specified in arrlist2 // using removeAll() method abs_set.removeAll(arrlist2); // print arrlist1 System.out.println("AbstractSet after " + "removeAll() operation : " + abs_set); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
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
// Java program to demonstrate // removeAll() method for Integer value import java.util.*; public class GFG1 { public static void main(String[] args) throws Exception { try { // Creating object of AbstractSet<Integer> AbstractSet<Integer> abs_set = new TreeSet<Integer>(); // Populating abs_set abs_set.add(1); abs_set.add(2); abs_set.add(3); abs_set.add(4); abs_set.add(5); // print abs_set System.out.println("AbstractSet before " + "removeAll() operation : " + abs_set); // Creating another object of ArrayList<Integer> Collection<Integer> arrlist2 = new ArrayList<Integer>(); arrlist2 = null; // print arrlist2 System.out.println("Collection Elements" + " to be removed : " + arrlist2); // Removing elements from AbstractSet // specified in arrlist2 // using removeAll() method abs_set.removeAll(arrlist2); // print arrlist1 System.out.println("AbstractSet after " + "removeAll() operation : " + abs_set); } catch (NullPointerException e) { System.out.println("Exception thrown : " + e); } } } |
AbstractSet before removeAll() operation : [1, 2, 3, 4, 5] Collection Elements to be removed : null Exception thrown : java.lang.NullPointerException
Recommended Posts:
- AbstractSet hashCode() Method in Java with Examples
- AbstractSet equals() Method in Java with Examples
- Set removeAll() method in Java with Examples
- List removeAll() method in Java with Examples
- SortedSet removeAll() method in Java with Examples
- LinkedBlockingDeque removeAll() method in Java with Examples
- CopyOnWriteArrayList removeAll() method in Java with Examples
- CopyOnWriteArraySet removeAll() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- LinkedTransferQueue removeAll() method in Java with Examples
- AbstractSet Class in Java with Examples
- AbstractSet add() method in Java with Example
- AbstractSet contains() method in Java with Example
- AbstractSet size() method in Java with Example
- AbstractSet isEmpty() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet



