ArrayDeque removeAll() method in Java (original) (raw)

Last Updated : 10 Apr, 2023

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)