Open In App

AbstractCollection removeAll() method in Java with Example

Last Updated : 24 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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]


Similar Reads

AbstractSequentialList removeAll() method in Java with Example
The removeAll() method of java.util.AbstractSequentialList 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 re
3 min read
LinkedHashSet removeAll() method in Java with Example
The removeAll() method of java.util.LinkedHashSet 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
3 min read
Stack removeAll() method in Java with Example
The Java.util.Stack.removeAll(Collection col) method is used to remove all the elements from the Stack, present in the collection specified. Syntax: Stack.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed from the Stack. Return Value: This method returns true
2 min read
HashSet removeAll() method in Java with Example
The removeAll() method of java.util.HashSet 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
TreeSet removeAll() method in Java with Example
The removeAll() method of java.util.TreeSet 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 thi
3 min read
AbstractCollection clear() Method in Java with Examples
The clear() method of Java AbstractCollection in Java is used to remove all of the elements from the Collection. Using the clear() method only clears all the element from the collection and does not delete the collection. In other words, it can be said that the clear() method is used to only empty an existing AbstractCollection. Syntax: AbstractCol
2 min read
AbstractCollection addAll() Method in Java with Examples
The addAll(Collection C) method of Java AbstractCollection is used to append all of the elements from a mentioned collection to this collection. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the collection. Ret
2 min read
AbstractCollection add() Method in Java with Examples
The add() method in Java AbstractCollection is used to add a specific element into a Collection. This method will add the element only if the specified element is not present in the Collection else the function will return False if the element is already present in the Collection. Syntax: AbstractCollection.add(Object element) Parameters: The param
2 min read
AbstractCollection isEmpty() Method in Java with Examples
The isEmpty() method of Java AbstractCollection is used to check and verify if a Collection is empty or not. It returns True if the Collection is empty else it returns False. Syntax: AbstractCollection.isEmpty() Parameters: The method does not take any parameter. Return Value: The function returns True if the collection is empty else it returns Fal
2 min read
AbstractCollection size() Method in Java with Examples
The size() method of Java AbstractCollection is used to get the size of the Collection or the number of elements present in the Collection.Syntax: AbstractCollection.size() Parameters: The method does not take any parameter.Return Value: The method returns the size or the number of elements present in the collection.Below programs illustrates the u
2 min read
Practice Tags :