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

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

The add method is used to add an element at the tail (end) of the ConcurrentLinkedDeque object.

Syntax

public boolean add(E e)

Parameters

The add method takes the element to be added to the deque as a parameter. This method throws a NullPointerException error if the argument is null.

Return value

The add method returns True if the element is successfully added to the deque.

Code

The code below demonstrates how to use the add method.

import java.util.concurrent.ConcurrentLinkedDeque;
class Add {
public static void main( String args[] ) {
ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();
deque.add("1");
System.out.println("The deque is " + deque);
deque.add("2");
System.out.println("The deque is " + deque);
}
}

Code explanation

In the code above:

  • In line 1, we import the ConcurrentLinkedDeque class.

  • In line 4, we create a ConcurrentLinkedDeque object with the name deque.

  • In line 5, we use the add method of the deque object to add an element ("1") to the end of the deque. Then, we print the deque.

  • In line 8, we use the add method to add an element ("2") to the end of the deque and print it. Now, the deque is [1,2].

Free Resources