The Wayback Machine - https://web.archive.org/web/20240618020216/https://www.geeksforgeeks.org/arraydeque-removeall-method-in-java/
Open In App

ArrayDeque removeAll() method in Java

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 contain no elements in common with the collection. This methods True if this ArrayDeque changed as a result of the calling this method. Syntax:

public boolean removeAll(Collection<? extends E> col)

Parameter: This method takes a parameter c which represents Collection of the elements we want to remove from this deque. Returns: This method returns True if this deque changed as a result of the calling this method. Exception: This method throws NullPointerException if the specified collection or any of its elements are null. Below programs illustrate removeAll() method of ArrayDeque: Program 1: Program to demonstrate removeAll() method on ArrayDeque which going to remove elements same as elements from a collection containing Numbers. 

Java




// Java Program Demonstrate removeAll()
// method of ArrayDeque
 
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
 
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
 
        // Add Number to list
        Numbers.add(23);
        Numbers.add(32);
        Numbers.add(45);
        Numbers.add(63);
 
        // print ArrayDeque before calling removeAll()
        System.out.println("Before calling removeAll()");
        print(Numbers);
 
        // create a collection of Number to
        // remove elements from ArrayDeque using removeAll()
        ArrayList<Integer> col = new ArrayList<Integer>();
 
        // add Numbers to collection
        col.add(45);
        col.add(44);
        col.add(63);
 
        // remove Numbers same as elements of collection
        // from ArrayDeque using removeAll()
        Numbers.removeAll(col);
 
        // print ArrayDeque after calling removeAll()
        System.out.println("After calling removeAll()");
        print(Numbers);
    }
 
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque<Integer> arDe)
    {
 
        arDe.forEach((n) -> System.out.print(n + " "));
 
        System.out.println();
    }
}


Output:

Before calling removeAll()
23 32 45 63 
After calling removeAll()
23 32

Program 2: Program to demonstrate removeAll() method on ArrayDeque which going to remove elements same as elements from collection of Students Names. 

Java




// Java Program Demonstrate removeAll()
// method of ArrayDeque
 
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
 
        // create an ArrayDeque which going to
        // contains a list of Student names which is actually
        // string values
        ArrayDeque<String> students = new ArrayDeque<String>();
 
        // Add Strings to list
        // each string represents student name
        students.add("Ram");
        students.add("Mohan");
        students.add("Sohan");
        students.add("Rabi");
 
        // print ArrayDeque before calling removeAll()
        System.out.println("Before calling removeAll()");
        print(students);
 
        // create a collection of Number to
        // remove elements from ArrayDeque using removeAll()
        LinkedList<String> col = new LinkedList<String>();
 
        // add Names in collection
        col.add("Rabi");
        col.add("Sohan");
 
        // remove the elements same as collection
        // from ArrayDeque using removeAll()
        students.removeAll(col);
 
        // print ArrayDeque
        System.out.println("After calling removeAll()");
        print(students);
    }
 
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque<String> arDe)
    {
 
        System.out.println("List of Students Name:");
 
        arDe.forEach((n) -> System.out.print(" | " + n + " | "));
 
        System.out.println("\n");
    }
}


Output:

Before calling removeAll()
List of Students Name:
 | Ram |  | Mohan |  | Sohan |  | Rabi | 

After calling removeAll()
List of Students Name:
 | Ram |  | Mohan |

Program 3: Program to demonstrate Exception thrown by removeAll() method. 

Java




// Java Program Demonstrate Exception thrown by removeAll()
// method of ArrayDeque
 
import java.util.*;
public class GFG {
 
    public static void main(String[] args)
    {
 
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
 
        // Add Number to list
        Numbers.add(223);
        Numbers.add(132);
        Numbers.add(345);
        Numbers.add(563);
 
        // create a collection of Number which is null
        ArrayList<Integer> col = null;
 
        try {
            // call removeAll() method
            Numbers.removeAll(col);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.NullPointerException

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



Similar Reads

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
ConcurrentSkipListSet removeAll() method in Java
The removeAll() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns removes from this set all of its elements that are contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the
3 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
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
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
AbstractCollection removeAll() method in Java with Example
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 Abstra
2 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