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

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 containsAll() method checks if all the elements of a specific collection object are present in the ConcurrentLinkedDeque object.

Syntax

public boolean containsAll(Collection<?> c)

Parameters

The containsAll() method accepts the collection whose presence will be checked for in the deque as an argument.

Return value

containsAll() returns true if all the elements of the passed collection are present in the deque.

Code

import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.ArrayList;
class containsAll {
public static void main( String args[] ) {
ConcurrentLinkedDeque<Integer> deque = new ConcurrentLinkedDeque<>();
deque.add(1);
deque.add(2);
deque.add(3);
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(3);
System.out.println("The deque is "+ deque);
System.out.println("\nlist1 is "+ list1);
System.out.println("If deque contains all elements of list1 : "+ deque.containsAll(list1));
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(4);
System.out.println("\nlist2 is "+ list2);
System.out.println("If deque contains all elements of list2 : "+ deque.containsAll(list2));
}
}

Explanation

In the code above:

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

  • In line 4: We create an object for the ConcurrentLinkedDeque class with the name deque.

  • In lines 6 to 8: We add three elements (1,2,3) to the created deque object.

  • In line 10: We create a new ArrayList object with the name list1 and add the elements 1,3 to it.

  • In line 16: We call the containsAll() method to check if all elements of list1 are present in the deque. In this case, the method returns true because all the elements (1,3) of list1 are present in the deque.

  • In line 18: We create a new ArrayList object with the name list2 and add the element 4 to it.

  • In line 22: We call the conainsAll() method again to check if all elements of list2 are present in the deque. In this case, the method returns false because element 4 of list2 is not present in the deque.

Free Resources