Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
deque
push()
communitycreator

What is the Deque.push() method in Java?

Sumit Sharma

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.

In this shot, we will discuss the Deque.push() method, where Deque means the Double Ended Queue, which means it contains push() and some other methods of LinkedList. The Deque.push() method is present in the Deque interface inside the java.util package.

The Deque.push(element) method is used to insert the element at the top of the Deque.

Syntax

void Deque.push()

Parameter

The Deque.push(element) method takes one parameter:

  • Element: This is the element to be inserted at top of the Deque.

Return

The Deque.push() method doesn’t return anything.

Code

Let’s look at the code snippet below.

import java.util.Deque;
import java.util.LinkedList;
class Main {
public static void main(String[] args){
Deque<Integer> objDeque = new LinkedList<Integer>();
objDeque.add(212);
objDeque.add(23);
objDeque.add(621);
objDeque.add(1280);
objDeque.add(3121);
objDeque.add(18297);
objDeque.add(791);
objDeque.push(7189);
objDeque.push(314);
System.out.println("Elements in the deque are: " + objDeque);
}
}
Using Deque.push() method in Java

Explanation

  • In lines 1 and 2, we imported the required package (Deque and LinkedList) from java.util.
  • In line 4, we created a Main class.
  • In line 5, we created a main function.
  • In line 6, we declared a Deque consisting of Integer type elements.
  • From lines 8 to 14, we inserted the elements in the deque by using the Deque.add() method, which inserts the element at the end of the Deque just like enqueue().
  • In lines 16 and 17, we inserted the elements in the front of deque using the Deque.push() method.
  • In line 18, we displayed the objDeque elements by implicitly calling its toString().

In this way, we can use the Deque.push() method in Java.

RELATED TAGS

java
deque
push()
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