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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
Numbers.add( 23 );
Numbers.add( 32 );
Numbers.add( 45 );
Numbers.add( 63 );
System.out.println("Before calling removeAll()");
print(Numbers);
ArrayList<Integer> col = new ArrayList<Integer>();
col.add( 45 );
col.add( 44 );
col.add( 63 );
Numbers.removeAll(col);
System.out.println("After calling removeAll()");
print(Numbers);
}
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayDeque<String> students = new ArrayDeque<String>();
students.add("Ram");
students.add("Mohan");
students.add("Sohan");
students.add("Rabi");
System.out.println("Before calling removeAll()");
print(students);
LinkedList<String> col = new LinkedList<String>();
col.add("Rabi");
col.add("Sohan");
students.removeAll(col);
System.out.println("After calling removeAll()");
print(students);
}
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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
Numbers.add( 223 );
Numbers.add( 132 );
Numbers.add( 345 );
Numbers.add( 563 );
ArrayList<Integer> col = null ;
try {
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)