Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ConcurrentLinkedDeque.removeAll() 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.

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 removeAll method removes all of the elements of the passed collection from ConcurrentLinkedDeque if present.

Syntax

boolean removeAll(Collection<?> c)

Parameters

This method takes the collection object to be removed from the deque as an argument.

NullPointerException is thrown if the passed collection contains a null element.

Return value

The removeAll method returns true if the deque changes as a result of the call. Otherwise, the method returns false.

Note: The method returns true if any one of the elements from the Collection is present and removed from the deque.

Code

The code below demonstrates how to use the removeAll() method.

import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.ArrayList;
class RemoveAll {
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.removeAll(list). Is deque changed - " + deque.removeAll(list));
System.out.println("\nThe deque is "+ deque);
}
}

Explanation

In the code above:

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

  • From 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.

  • In lines 9-11: We create a new ArrayList object with the name list and add two elements (1,4) to the list object with the add method.

  • In line 15: We use the removeAll method to remove all the elements of list from the deque. The element 1 in the list is present in the deque, so it is removed. The element 4 is not present in the deque, so it is not removed. After we call the removeAll method, the content of the deque object changes, so we get a return value of true.

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