Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ConcurrentLinkedDeque.addFirst() 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 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);
}
}
An example of the ConcurrentlyLinkedDeque.addFirst() method

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 addFirst method of the deque object to add an element ("3") to the beginning of the deque and print it.

  • In line 8: We use the addFirst method to add an element ("2") to the beginning of the deque and print it.

  • In line 11: We use the addFirst method to add an element ("1") to the beginning of the deque and print it.

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