How to implement Stack and Queue using ArrayDeque in Java
Last Updated :
24 Aug, 2022
ArrayDeque in Java
The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue. ArrayDeque class implements Queue and Deque interface which provides it a lot of methods and functionalities.

ArrayDeque Class in JAVA
How to use ArrayDeque
ArrayDeque is a class in Java Programming Language that implements the Queue and Deque interface that further extends the Collection Interface. ArrayDeque has all the methods of Queue and Deque such as add(), remove(), addFirst(), addLast(), removeFirst(), removeLast(), and many others inherited from these interfaces. It is very easy to use ArrayDeque class because of the ease of these methods provided in Java.
Insertion at the front
offerFirst() and addFirst() methods can be used to add element(s) at the front of the ArrayDeque. The main difference in between these two methods is that offerFirst() returns True if the element is added and returns False otherwise whereas addFirst() does not return a value.
Code implementation of the above methods:
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
public static void main(String[] args)
{
Deque<Integer> arrayDeque
= new ArrayDeque<Integer>();
arrayDeque.addFirst( 15 );
arrayDeque.addFirst( 19 );
System.out.println(
"Deque After Inserting using addFirst(): "
+ arrayDeque);
arrayDeque.offerFirst( 17 );
arrayDeque.offerFirst( 22 );
System.out.println(
"Deque After Inserting using offerFirst(): "
+ arrayDeque);
}
}
|
Output
Deque After Inserting using addFirst(): [19, 15]
Deque After Inserting using offerFirst(): [22, 17, 19, 15]
Insertion at the back
offerLast() and addLast() methods can be used to add element(s) at the back of the ArrayDeque. The main difference in between these two methods is that offerLast() returns True if the element is added and returns False otherwise whereas addLast() does not return a value.
Code implementation of the above methods
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
public static void main(String[] args)
{
Deque<Integer> arrayDeque
= new ArrayDeque<Integer>();
arrayDeque.addLast( 15 );
arrayDeque.addLast( 19 );
System.out.println(
"Deque After Inserting using addLast(): "
+ arrayDeque);
arrayDeque.offerLast( 17 );
arrayDeque.offerLast( 22 );
System.out.println(
"Deque After Inserting using offerLast(): "
+ arrayDeque);
}
}
|
Output
Deque After Inserting using addLast(): [15, 19]
Deque After Inserting using offerLast(): [15, 19, 17, 22]
Deletion from the front
pollFirst() and removeFirst() can be used to remove the front element(s) from the ArrayDeque. The difference between these elements is that pollFirst() returns NULL if the ArrayDeque is empty whereas removeFirst() throws NoSuchElementException if the ArrayDeque is empty.
Code implementation for the above methods
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
public static void main(String[] args)
{
Deque<Integer> arrayDeque
= new ArrayDeque<Integer>();
arrayDeque.add( 15 );
arrayDeque.add( 19 );
arrayDeque.add( 17 );
arrayDeque.add( 22 );
System.out.println( "Deque After Insertion: "
+ arrayDeque);
Integer i1 = arrayDeque.removeFirst();
System.out.println( "Deleted Element: " + i1);
System.out.println(
"Deque after Deletion using removeFirst(): "
+ arrayDeque);
Integer i2 = arrayDeque.pollFirst();
System.out.println( "Deleted Element: " + i2);
System.out.println(
"Deque after Deletion using pollFirst(): "
+ arrayDeque);
}
}
|
Output
Deque After Insertion: [15, 19, 17, 22]
Deleted Element: 15
Deque after Deletion using removeFirst(): [19, 17, 22]
Deleted Element: 19
Deque after Deletion using pollFirst(): [17, 22]
Deletion from the end
pollLast() and removeLast() can be used to remove the back element(s) from the ArrayDeque. The difference between these elements is that pollLast() returns NULL if the ArrayDeque is empty whereas removeLast() throws NoSuchElementException if the ArrayDeque is empty.
Code implementation for the above methods
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
public static void main(String[] args)
{
Deque<Integer> arrayDeque
= new ArrayDeque<Integer>();
arrayDeque.add( 15 );
arrayDeque.add( 19 );
arrayDeque.add( 17 );
arrayDeque.add( 22 );
System.out.println( "Deque After Insertion: "
+ arrayDeque);
Integer i1 = arrayDeque.removeLast();
System.out.println( "Deleted Element: " + i1);
System.out.println(
"Deque after Deletion using removeLast(): "
+ arrayDeque);
Integer i2 = arrayDeque.pollLast();
System.out.println( "Deleted Element: " + i2);
System.out.println(
"Deque after Deletion using pollLast(): "
+ arrayDeque);
}
}
|
Output
Deque After Insertion: [15, 19, 17, 22]
Deleted Element: 22
Deque after Deletion using removeLast(): [15, 19, 17]
Deleted Element: 17
Deque after Deletion using pollLast(): [15, 19]
How to implement Stack using ArrayDeque
ArrayDeque can be easily used as a Stack as it provides the option to add and remove elements from both ends. There exists push() and pop() methods that make ArrayDeque favorable to be implemented as a Stack as it mimics the working of a usual Stack.
Code implementation of Stack using ArrayDeque
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
public static void main(String[] args)
{
Deque<Integer> stack = new ArrayDeque<Integer>();
stack.push( 17 );
stack.push( 19 );
stack.push( 15 );
System.out.println( "Stack after insertion: "
+ stack);
stack.pop();
System.out.println( "Stack after deletion: "
+ stack);
stack.pop();
System.out.println( "Stack after deletion: "
+ stack);
}
}
|
Output
Stack after insertion: [15, 19, 17]
Stack after deletion: [19, 17]
Stack after deletion: [17]
How to implement Queue using ArrayDeque
ArrayDeque implements the Queue interface so it already has all the functionality provided by the interface. So, ArrayDeque can be used to perform all the normal queue operations using add() and remove() methods.
Code implementation of Queue using ArrayDeque
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
public static void main(String[] args)
{
Deque<Integer> queue = new ArrayDeque<Integer>();
queue.add( 15 );
queue.add( 17 );
queue.add( 22 );
System.out.println( "Queue after insertion: "
+ queue);
queue.remove();
System.out.println( "Queue after deletion: "
+ queue);
queue.remove();
System.out.println( "Queue after deletion: "
+ queue);
}
}
|
Output
Queue after insertion: [15, 17, 22]
Queue after deletion: [17, 22]
Queue after deletion: [22]
Similar Reads
Implement Stack and Queue using Deque
Deque also known as double ended queue, as name suggests is a special kind of queue in which insertions and deletions can be done at the last as well as at the beginning. A link-list representation of deque is such that each node points to the next node as well as the previous node. So that insertio
15 min read
Implement enqueue and dequeue using only two stacks in JavaScript ?
In this article, we will implement queue's operations (enqueue and dequeue) using only two stacks (in the form of plain arrays) in JavaScript. Before directly jumping into understanding the problem statement let us understand briefly what exactly a stack is and also what exactly a queue is. A stack
6 min read
How to efficiently implement k Queues in a single array?
Given an array of size n, the task is to implement k queues using the array. enqueue(qn, x) : Adds the element x into the queue number qn dequeue(qn, x) : Removes the front element from queue number qn isFull(qn) : Checks if the queue number qn is fullisEmpty(qn) : Checks if the queue number qn is e
15+ min read
How to implement stack using priority queue or heap?
How to Implement stack using a priority queue(using min heap)? Asked In: Microsoft, Adobe. Solution: In the priority queue, we assign priority to the elements that are being pushed. A stack requires elements to be processed in the Last in First Out manner. The idea is to associate a count that dete
6 min read
How to implement a Stack using list in C++ STL
In this article, we will discuss how to implement a Stack using list in C++ STL. Stack is a linear data structure which follows. LIFO(Last In First Out) or FILO(First In Last Out). It mainly supports 4 major operations:1. Push: Push an element into the stack.2. Pop: Removes the element by following
3 min read
C++ Program to Implement Stack using array
Stack is the fundamental data structure that can operates the under the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Implementing the stack using the array is one of the most straightforward methods in the terms of the both
4 min read
C++ Program to Implement Queue using Array
A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu
8 min read
Implement k stacks in an array
Given an array of size n, the task is to implement k stacks using a single array. We mainly need to perform the following type of queries on the stack. push(x, i) : This operations pushes the element x into stack i pop(i) : This operation pops the top of stack i Here i varies from 0 to k-1 Naive App
15+ min read
JavaScript program to implement queue using stack
A queue is a First In First Out (FIFO) data structure, in which the first element added to the queue is the first one to be removed. The different operations associated with Queue include Enqueue, Dequeue etc. A stack is a Last In, First Out (LIFO) data structure, in which the last element added to
3 min read
JavaScript program to implement stack using queue
In this article, we implement a JavaScript program to make a stack using a queue data structure. It provides essential stack methods like push(), pop(), and peek(), isEmpty() operations, utilizing either one or two queues to simulate the behavior of a stack. Examples: Input:push(2)push(3)pop()peek()
4 min read