What is the ConcurrentLinkedDeque.addFirst() 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 addFirst method of the ConcurrentLinkedDeque class adds an element to the beginning of the deque object.
Syntax
public void addFirst(E e);
Parameters
This method takes the element to be added as an argument.
Return value
The addFirst method doesn’t return a value.
Code
The code below demonstrates how to use the addFirst method.
import java.util.concurrent.ConcurrentLinkedDeque;class AddFirst {public static void main( String args[] ) {ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();deque.addFirst("3");System.out.println("The deque is " + deque);deque.addFirst("2");System.out.println("The deque is " + deque);deque.addFirst("1");System.out.println("The deque is " + deque);}}
Explanation
In the code above:
-
In line 1: We import the
ConcurrentLinkedDequeclass. -
In line 4: We create a
ConcurrentLinkedDequeobject with the namedeque. -
In line 5: We use the
addFirstmethod of thedequeobject to add an element ("3") to the beginning of thedequeand print it. -
In line 8: We use the
addFirstmethod to add an element ("2") to the beginning of thedequeand print it. -
In line 11: We use the
addFirstmethod to add an element ("1") to the beginning of thedequeand print it.