Java ArrayDeque retainAll() Method with Examples



Description

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

Declaration

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

public boolean retainAll​(Collection<?> c)

Parameters

c − The collection containing elements to be retained 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 retainAll() method with Integers. We're creating an ArrayDeque of Integers, adding some elements, print it and then use retainAll() method to retain few elements. As ArrayDeque is modified it is printed to check if specified elements are retained 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 retain two common elements
      System.out.println("ArrayDeque modified:  " + deque.retainAll(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 = [30, 20]

Example #2

The following example shows the usage of Java ArrayDeque retainAll() method with Strings. We're creating an ArrayDeque of Strings, adding some elements, print it and then use retainAll() method to retain few elements. As ArrayDeque is modified it is printed to check if specified elements are retained 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 retain two common elements
      System.out.println("ArrayDeque modified:  " + deque.retainAll(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 = [B, C]

Example #3

The following example shows the usage of Java ArrayDeque retainAll() method with Student objects. We're creating an ArrayDeque of Student objects, adding some elements, print it and then use retainAll() method to retain few students. As ArrayDeque is modified it is printed to check if specified students are retained 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 retaining two students from deque
      System.out.println("Student removed : " + deque.retainAll(
         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 = [[ 2, Robert ], [ 3, Adam ]]
java_util_arraydeque.htm
Advertisements