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 JAVAHow 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 { // Driver Code public static void main(String[] args) { // Creating an ArrayDeque Deque<Integer> arrayDeque = new ArrayDeque<Integer>(); // Adding to front of ArrayDeque // using addFirst() method arrayDeque.addFirst(15); arrayDeque.addFirst(19); // Printing Arraydeque elements System.out.println( "Deque After Inserting using addFirst(): " + arrayDeque); // Adding to front of ArrayDeque // using offerFirst() method arrayDeque.offerFirst(17); arrayDeque.offerFirst(22); System.out.println( "Deque After Inserting using offerFirst(): " + arrayDeque); } }
OutputDeque 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 { // Driver Code public static void main(String[] args) { // Creating an ArrayDeque Deque<Integer> arrayDeque = new ArrayDeque<Integer>(); // Adding at the end of ArrayDeque // using addLast() method arrayDeque.addLast(15); arrayDeque.addLast(19); // Printing ArrayDeque elements System.out.println( "Deque After Inserting using addLast(): " + arrayDeque); // Adding at the end of ArrayDeque // using offerLast() method arrayDeque.offerLast(17); arrayDeque.offerLast(22); System.out.println( "Deque After Inserting using offerLast(): " + arrayDeque); } }
OutputDeque 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 { // Driver Code public static void main(String[] args) { // Creating an ArrayDeque Deque<Integer> arrayDeque = new ArrayDeque<Integer>(); // Adding elements to the arrayDeque arrayDeque.add(15); arrayDeque.add(19); arrayDeque.add(17); arrayDeque.add(22); // Printing the elements of ArrayDeque System.out.println("Deque After Insertion: " + arrayDeque); // Removing element from the front // using removeFirst() method Integer i1 = arrayDeque.removeFirst(); System.out.println("Deleted Element: " + i1); // Printing the ArrayDeque System.out.println( "Deque after Deletion using removeFirst(): " + arrayDeque); // Removing element from the front // using pollFirst() method Integer i2 = arrayDeque.pollFirst(); System.out.println("Deleted Element: " + i2); System.out.println( "Deque after Deletion using pollFirst(): " + arrayDeque); } }
OutputDeque 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 { // Driver Code public static void main(String[] args) { // Creating an ArrayDeque Deque<Integer> arrayDeque = new ArrayDeque<Integer>(); // Adding elements to the arrayDeque arrayDeque.add(15); arrayDeque.add(19); arrayDeque.add(17); arrayDeque.add(22); // Printing the elements of ArrayDeque System.out.println("Deque After Insertion: " + arrayDeque); // Removing element from the end // using removeLast() method Integer i1 = arrayDeque.removeLast(); System.out.println("Deleted Element: " + i1); // Printing the ArrayDeque System.out.println( "Deque after Deletion using removeLast(): " + arrayDeque); // Removing element from the end // using pollLast() method Integer i2 = arrayDeque.pollLast(); System.out.println("Deleted Element: " + i2); System.out.println( "Deque after Deletion using pollLast(): " + arrayDeque); } }
OutputDeque 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 { // Driver Code public static void main(String[] args) { // Creating an ArrayDeque to use // as a Stack Deque<Integer> stack = new ArrayDeque<Integer>(); // Inserting elements in the Stack // using push() operation stack.push(17); stack.push(19); stack.push(15); // Printing the elements System.out.println("Stack after insertion: " + stack); // Removing elements from the Stack // using pop() operation stack.pop(); System.out.println("Stack after deletion: " + stack); stack.pop(); System.out.println("Stack after deletion: " + stack); } }
OutputStack 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 { // Driver Code public static void main(String[] args) { // Creating a Queue using ArrayDeque Deque<Integer> queue = new ArrayDeque<Integer>(); // Adding the elements to the Queue queue.add(15); queue.add(17); queue.add(22); // Printing the elements of the Queue System.out.println("Queue after insertion: " + queue); // Removing elements from the queue queue.remove(); System.out.println("Queue after deletion: " + queue); queue.remove(); System.out.println("Queue after deletion: " + queue); } }
OutputQueue after insertion: [15, 17, 22] Queue after deletion: [17, 22] Queue after deletion: [22]
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read