What is the ConcurrentLinkedDeque.containsAll() method in Java?
ConcurrentLinkedDequeis a thread-safe, unbounded deque. Thenullvalue is not permitted as an element. We can useConcurrentLinkedDequewhen 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
ConcurrentLinkedDequeandArrayListclasses. -
In line 4: We create an object for the
ConcurrentLinkedDequeclass with the namedeque. -
In lines 6 to 8: We add three elements (
1,2,3) to the createddequeobject. -
In line 10: We create a new
ArrayListobject with the namelist1and add the elements1,3to it. -
In line 16: We call the
containsAll()method to check if all elements oflist1are present in thedeque. In this case, the method returnstruebecause all the elements (1,3) oflist1are present in thedeque. -
In line 18: We create a new
ArrayListobject with the namelist2and add the element4to it. -
In line 22: We call the
conainsAll()method again to check if all elements oflist2are present in thedeque. In this case, the method returnsfalsebecause element4oflist2is not present in thedeque.