Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ConcurrentLinkedDeque.retainAll() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

ConcurrentLinkedDeque is a thread-safe, unbounded deque. The null value is not permitted as an element. We can use ConcurrentLinkedDeque when multiple threads share a single deque.

The retainAll method removes all the elements of the ConcurrentLinkedDeque object that are not present in the passed collection.

Syntax

The syntax of the retainAll method is given below:

public boolean retainAll(Collection<?> c)

Parameters

This method takes the collection object to be retained in the deque object as an argument.

NullPointerException is thrown if the passed collection contains null elements.

The method throws ClassCastException if any one of the elements in the passed collection is incompatible with the specified collection.

Return value

The retainAll method returns true if the deque changes as a result of the call, meaning that any element is removed from the deque because it is not present in the passed collection. Otherwise, the method returns false.

Code

The code below demonstrates how to use the retainAll method.

import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.ArrayList;
class RetainAll {
public static void main( String args[] ) {
ConcurrentLinkedDeque<Integer> deque = new ConcurrentLinkedDeque<>();
deque.add(1);
deque.add(2);
deque.add(3);
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(4);
System.out.println("The deque is "+ deque);
System.out.println("The list is "+ list);
System.out.println("\nCalling deque.retainAll(list). Is deque changed - " + deque.retainAll(list));
System.out.println("\nThe deque is "+ deque);
}
}

Explanation

In the code above:

  • Lines 1-2: We import the ConcurrentLinkedDeque and ArrayList classes.

  • Lines 5-8: We create a new ConcurrentLinkedDeque object with the name deque and add three elements (1,2,3) to the deque object with the add method.

  • Lines 10-12: We create a new ArrayList object with the name list and add two elements (1,4) to the list object with the add method.

  • Line 16: We use the retainAll method to only retain the elements that are present in list. In our case, the elements of the list and deque are as follows:

Elements of list -- 1,4
Elements of deque -- 1,2,3

The retainAll method will:

  • Check if element 1 of deque is present in list. list contains 1, so 1 is retained in the deque.

  • Check if element 2 of deque is present in list. list doesn’t contain 2, so 2 is removed from the deque.

  • Check if element 3 of deque is present in list. The list doesn’t contain 3, so 3 is removed from the deque.

The resulting deque will be [1].

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring