Java ArrayDeque removeAll() Method with Examples



Description

The java ArrayDeque removeAll(Collection<?> c) method removes all of common elements present in the arraydeque object and the provided collection's elements. This method modifies the arraydeque object.

Declaration

Following is the declaration for java.util.ArrayDeque.removeAll(Collection<?> c) method

public boolean removeAll​(Collection<?> c)

Parameters

c − The collection containing elements to be removed from this collection.

Return Value

true if this arraydeque is changed as a result of the call.

Exception

NullPointerException − if this arraydeque contains one or more null elements and the specified collection does not support null elements, or if the specified collection is null.

Example #1

The following example shows the usage of Java ArrayDeque removeAll() method with Integers. We're creating an ArrayDeque of Integers, adding some elements, print it and then use removeAll() method to remove few elements. As ArrayDeque is modified it is printed to check if specified elements are removed or not.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Arrays;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      ArrayDeque<Integer> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(25);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // it will remove two common elements
      System.out.println("ArrayDeque modified:  " + deque.removeAll(Arrays.asList(11,30,20,12)));
	  
      // let us print all the elements available in deque again
      System.out.println("ArrayDeque = " + deque);
   }
}

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [25, 30, 20, 18]
ArrayDeque modified:  true
ArrayDeque = [25, 18]

Example #2

The following example shows the usage of Java ArrayDeque removeAll() method with Strings. We're creating an ArrayDeque of Strings, adding some elements, print it and then use removeAll() method to remove few elements. As ArrayDeque is modified it is printed to check if specified elements are removed or not.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Arrays;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      ArrayDeque<String> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add("A");
      deque.add("B");
      deque.add("C");
      deque.add("D");        

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // it will remove two common elements
      System.out.println("ArrayDeque modified:  " + deque.removeAll(Arrays.asList("E","B","C","F")));
	  
      // let us print all the elements available in deque again
      System.out.println("ArrayDeque = " + deque);
   }
}

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [A, B, C, D]
ArrayDeque modified:  true
ArrayDeque = [A, D]

Example #3

The following example shows the usage of Java ArrayDeque removeAll() method with Student objects. We're creating an ArrayDeque of Student objects, adding some elements, print it and then use removeAll() method to remove few students. As ArrayDeque is modified it is printed to check if specified students are removed or not.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Arrays;

public class ArrayDequeDemo {
   public static void main(String[] args) {
      
      // create an empty array deque
      ArrayDeque<Student> deque = new ArrayDeque<>();

      // use add() method to add elements in the deque
      deque.add(new Student(1, "Julie"));
      deque.add(new Student(2, "Robert"));
      deque.add(new Student(3, "Adam"));       

      // let us print all the elements available in deque
      System.out.println("ArrayDeque = " + deque);

      // it will return true after removing two students from deque
      System.out.println("Student removed : " + deque.removeAll(
         Arrays.asList(new Student(2, "Robert"),new Student(3, "Adam"))));
	  
      // let us print all the elements available in deque again
      System.out.println("ArrayDeque = " + deque);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Student removed :true
ArrayDeque = [[ 1, Julie ]]
java_util_arraydeque.htm
Advertisements