Open In App
Related Articles

AbstractCollection removeAll() method in Java with Example

Improve Article
Improve
Save Article
Save
Like Article
Like

The Java.util.AbstractCollection.removeAll(Collection col) method is used to remove all the elements from the AbstractCollection, present in the collection specified.

Syntax:

AbstractCollection.removeAll(Collection col)

Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed from the AbstractCollection.

Return Value: This method returns true if the AbstractCollection is altered due to the operation at all, else False.

Exception: The method throws NullPointerException if the specified collection is null.

Below programs illustrate the Java.util.AbstractCollection.removeAll(Collection col) method:

Program 1:




// Java code to illustrate removeAll()
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
  
        // Creating an empty AbstractCollection
        AbstractCollection<String> collection
            = new ArrayList<String>();
  
        // Use add() method to add
        // elements in the AbstractCollection
        collection.add("Geeks");
        collection.add("for");
        collection.add("Geeks");
        collection.add("10");
        collection.add("20");
  
        // Output the AbstractCollection
        System.out.println("AbstractCollection: "
                           + collection);
  
        // Creating an empty AbstractCollection
        AbstractCollection<String> colcollection
            = new ArrayList<String>();
  
        // Use add() method to add
        // elements in the AbstractCollection
        colcollection.add("Geeks");
        colcollection.add("for");
        colcollection.add("Geeks");
  
        // Remove the head using remove()
        boolean changed
            = collection.removeAll(colcollection);
  
        // Print the result
        if (changed)
            System.out.println("Collection removed");
        else
            System.out.println("Collection not removed");
  
        // Print the final AbstractCollection
        System.out.println("Final AbstractCollection: "
                           + collection);
    }
}


Output:

AbstractCollection: [Geeks, for, Geeks, 10, 20]
Collection removed
Final AbstractCollection: [10, 20]

Program 2:




// Java code to illustrate removeAll()
import java.util.*;
  
public class AbstractCollectionDemo {
    public static void main(String args[])
    {
  
        // Creating an empty AbstractCollection
        AbstractCollection<Integer> collection
            = new ArrayList<Integer>();
  
        // Use add() method to
        // add elements in the AbstractCollection
        collection.add(1);
        collection.add(2);
        collection.add(3);
        collection.add(10);
        collection.add(20);
  
        // Output the AbstractCollection
        System.out.println("AbstractCollection: "
                           + collection);
  
        // Creating an empty AbstractCollection
        AbstractCollection<Integer> colcollection
            = new ArrayList<Integer>();
  
        // Use add() method to add elements
        // in the AbstractCollection
        colcollection.add(30);
        colcollection.add(40);
        colcollection.add(50);
  
        // Remove the head using remove()
        boolean changed
            = collection.removeAll(colcollection);
  
        // Print the result
        if (changed)
            System.out.println("Collection removed");
        else
            System.out.println("Collection not removed");
  
        // Print the final AbstractCollection
        System.out.println("Final AbstractCollection: "
                           + collection);
    }
}


Output:

AbstractCollection: [1, 2, 3, 10, 20]
Collection not removed
Final AbstractCollection: [1, 2, 3, 10, 20]

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 24 Dec, 2018
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials