How to Use Deque as a Stack in Java? Last Updated : 29 Jan, 2024 Comments Improve Suggest changes Like Article Like Report The Deque extends the Queue interface and provides additional methods to support operation at both ends. This interface is part of the java.util package. The Deque interface does not strictly enforce the order of elements, meaning that different implementations may behave differently. The standard implementations of Deque in Java include ArrayDeque and LinkedList. In this article, we will check How to use Deque as a Stack in Java. Implement a Stack using DequeIn Java, you can use the Deque interface to implement a stack data structure. A Deque as "double-ended queue" and it provides methods to add and remove the elements from both ends. By using only, the methods from the end (push and pop), you can effectively use a Deque as a Stack. Example: Java // Java Program to Implement a // Stack using Deque import java.io.*; import java.util.ArrayDeque; import java.util.Deque; // Driver Class class GFG { // main function public static void main (String[] args) { // Create the Deque Deque<Integer> stackDeque = new ArrayDeque<>(); // Push the element into stackDeque stackDeque.push(11); stackDeque.push(12); stackDeque.push(13); stackDeque.push(14); // Print the stackDeque System.out.println("Deque Stack: "+stackDeque); // Print the peek element in the stack int peekElement = stackDeque.peek(); System.out.println("Peeked Element: "+peekElement); // Check the stack is empty or not boolean isEmpty = stackDeque.isEmpty(); System.out.println("Is stack is Empty? "+isEmpty); // Pop the element into the stack int popElement = stackDeque.pop(); System.out.println("Popped Element: "+popElement); // Print the updated stack System.out.println("Updated stack: "+stackDeque); } } OutputDeque Stack: [14, 13, 12, 11] Peeked Element: 14 Is stack is Empty? false Popped Element: 14 Updated stack: [13, 12, 11] Explanation of the above Program: In this program, we can implement the basic operation of stack are push, pop, peek and isEmpty into the program. push() - To add elements to the top of the stack.pop() - method is used to remove the retrieve the elements from the top of the stack.peek() - It returns the top element of the stack.isEmpty() - return true if the elements in the stack otherwise false Comment More infoAdvertise with us Next Article How to Use Deque as a Stack in Java? M maheshkadambala Follow Improve Article Tags : Java Java Programs Java-Collections java-deque Java-Stack Java Examples +2 More Practice Tags : JavaJava-Collections Similar Reads How to Implement Stack in Java using LinkedList and Generics? Prerequisites: Generics in JavaLinkedList in JavaWhat is Stack? Stack is a linear Data Structure that follows LIFO(Last In First Out) order while performing its operations. The main operations that are performed on the stack are mentioned below: push() : Â inserts an element at beginning of the stack 6 min read Java Program to Implement ArrayDeque API The ArrayDeque in Java provides how to use resizable-array additionally to the implementation of the Deque interface. It is also referred to as Array Double Ended Queue or Array Deck. This is a special quite array that grows and allows users to feature or remove a component from each side of the que 3 min read Java Program to Implement Stack API A stack is a linear data structure that follows a particular order in which insertion/deletion operations are performed. The order is either LIFO(Last In First Out) or FILO(First In Last Out). Stack uses the push() function in order to insert new elements into the Stack and pop() function in order t 3 min read Java Program to Implement ConcurrentLinkedDeque API ConcurrentLinkedDeque class in Java is an unbounded concurrent deque that stores its elements as linked nodes where each node contains the address of the previous as well as next nodes. It belongs to java.util.concurrent package. This class is a member of the Java Collections Framework. It also exte 3 min read How to Implement a FIFO (First-In-First-Out) Queue in Java? FIFO stands for First In, First Out, which is a common way of organizing and manipulating Data Structure. In FIFO, the first element that is added to the data structure is the same first element that is to be removed first. Queue is a FIFO-based Data Structure. Here, the insert operation is called E 3 min read Like