The Wayback Machine - https://web.archive.org/web/20240618020152/https://www.geeksforgeeks.org/set-removeall-method-in-java-with-examples/
Open In App

Set removeAll() method in Java with Examples

Last Updated : 30 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The removeAll() method of java.util.Set interface 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: This method throws NullPointerException if this set contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null.

Below are the examples to illustrate the removeAll() method.

Example 1:




// Java program to demonstrate
// removeAll() method for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
  
        try {
            // Creating object of Set
            Set<Integer> set1 = new HashSet<Integer>();
  
            // Populating set1
            set1.add(1);
            set1.add(2);
            set1.add(3);
            set1.add(4);
            set1.add(5);
  
            // print set1
            System.out.println("Set before removeAll() operation : "
                               + set1);
  
            // Creating another object of Set
            Set<Integer> set2 = new HashSet<Integer>();
            set2.add(1);
            set2.add(2);
            set2.add(3);
  
            // print set2
            System.out.println("Collection Elements to be removed : "
                               + set2);
  
            // Removing elements from set
            // specified in set2
            // using removeAll() method
            set1.removeAll(set2);
  
            // print set1
            System.out.println("Set after removeAll() operation : "
                               + set1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Set before removeAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be removed : [1, 2, 3]
Set 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[] argv) throws Exception
    {
        try {
  
            // Creating object of Set
            Set<Integer> set1 = new HashSet<Integer>();
  
            // Populating set1
            set1.add(1);
            set1.add(2);
            set1.add(3);
            set1.add(4);
            set1.add(5);
  
            // print set1
            System.out.println("Set before removeAll() operation : "
                               + set1);
  
            // Creating another object of Set<Integer>
            Set<Integer> set2 = null;
  
            // print set2
            System.out.println("Collection Elements to be removed : "
                               + set2);
  
            System.out.println("\nTrying to pass "
                               + "null as a specified element\n");
  
            // Removing elements from set
            // specified in set2
            // using removeAll() method
            set1.removeAll(set2);
  
            // print set1
            System.out.println("Set after removeAll() operation : "
                               + set1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Set before removeAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be removed : null

Trying to pass null as a specified element

Exception thrown : java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#removeAll(java.util.Collection)



Previous Article
Next Article

Similar Reads

ArrayList removeAll() method in Java with Examples
The removeAll() method of java.util.ArrayList class is used to remove from this list 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 list. Returns Value: This method returns true if
3 min read
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
3 min read
List removeAll() method in Java with Examples
This method is used to remove all the elements present in the collection from the specified list. Syntax: boolean removeAll(Collection c) Parameters: This method has only argument, collection of which elements are to be removed from the given list. Returns: This method returns True if elements are removed and list changes. Below programs show the i
2 min read
CopyOnWriteArraySet removeAll() method in Java with Examples
The removeAll() method of CopyonWriteArraySet method removes all the elements of this CopyOnWriteArraySet that are present in the specified collection. That means elements which are common in both the collections are removed from this CopyOnWriteArraySet. Syntax: public boolean removeAll(Collection&lt;E&gt; c) Parameters: This method accepts a para
2 min read
LinkedTransferQueue removeAll() method in Java with Examples
The removeAll() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to remove from this queue 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
3 min read
LinkedBlockingDeque removeAll() method in Java with Examples
The removeAll() method of LinkedBlockingDeque is an in-built function is Java which is used to remove from this deque all of its elements that are contained in the specified collection. That means, all the common elements these two collections are removed from this deque.Syntax: public boolean removeAll(Collection c) Parameters: This method takes c
2 min read
SortedSet removeAll() method in Java with Examples
The removeAll() method of SortedSet interface is used to remove from this SortedSet 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 SortedSet.Returns Value: This method returns true
3 min read
Java Collection removeAll() Method with Examples
The removeAll() method of Java Collection removes those elements from the collection that are contained in the collection which is given as an argument to function. Syntaxboolean removeAll(Collection&lt;?&gt; c);Parameters:c is the collection containing elements that are removed from the calling collection.Return Type: It returns a boolean value. I
2 min read
Vector removeAll() Method in Java
The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified. Syntax: Vector.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed from the vector. Return Value: This method returns
2 min read
ArrayDeque removeAll() method in Java
The removeAll() method of ArrayDeque is used to remove all the elements which is common in both ArrayDeque and the collection passed as parameter. This method first collect all the elements of Collection and then starts removing the elements from ArrayDeque which are same as elements of Collection.After this method execution, this ArrayDeque will c
4 min read