Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

push
java
communitycreator

What is the ConcurrentLinkedDeque.push() 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.

Overview

The push(element) method adds an element at the head or top of the ConcurrentLinkedDeque object.

Note: The 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.

Syntax

public void push(E element);

Parameter(s)

This method takes the element to be added to the deque as an argument. If the argument is null, then a NullPointerException is thrown.

Returns

This method doesn’t return any value.

Code

The code below demonstrates the use of the push(element) method:

import java.util.concurrent.ConcurrentLinkedDeque;
class Push {
public static void main( String args[] ) {
ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();
deque.push("3");
System.out.println("The deque is " + deque);
deque.push("2");
System.out.println("The deque is " + deque);
deque.push("1");
System.out.println("The deque is " + deque);
}
}
Example of the push() method

Explanation

In the code above:

  • Line 1: We import the ConcurrentLinkedDeque class.

  • Line 4: We create a ConcurrentLinkedDeque object with the name deque.

  • Lines 5 and 6: We use the push() method of the deque object to add an element ("3") to the beginning of the deque and print it. Now the deque is [3].

  • Lines 8 and 9: We use the push() method to add an element ("2") to the beginning of the deque and print it. Now the deque is [2,3].

  • Lines 11 and 12: We use the push() method to add an element ("1") to the beginning of the deque. Now the deque is [1,2,3].

RELATED TAGS

push
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