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

AbstractSet removeAll() Method in Java with Examples

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

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);
        }
    }
}


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




// 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);
        }
    }
}


Output:

AbstractSet before removeAll() operation : [1, 2, 3, 4, 5]
Collection Elements to be removed : null
Exception thrown : java.lang.NullPointerException


Previous Article
Next Article

Similar Reads

AbstractSet hashCode() Method in Java with Examples
The AbstractSet.hashCode() method in Java AbstractSet is used to fetch the hash code value of a particular this AbstractSet. A set consists of a number of buckets to store the elements. Each bucket has a unique identity and when an element is inserted into a bucket, its hashcode is matched with the identifier of the bucket, and if its a match the e
2 min read
AbstractSet equals() Method in Java with Examples
The AbstractSet.equals() method in Java AbstractSet is used to check for equality between two sets. It verifies whether the elements of one set passed as a parameter is equal to the elements of this set or not. Syntax: AbstractSet1.equals(AbstractSet2) Parameters: The method accepts one parameter AbstractSet2 of abstract set type and refers to the
2 min read
AbstractSet Class in Java with Examples
AbstractSet class in Java is a part of the Java Collection Framework which implements the Collection interface and extends the AbstractCollection class. It provides a skeletal implementation of the Set interface. This class does not override any of the implementations from the AbstractCollection class, but merely adds implementations for the equals
9 min read
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
Set removeAll() method in Java with Examples
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 thi
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