Skip to content
Related Articles
Open in App
Not now

Related Articles

How to implement Stack and Queue using ArrayDeque in Java

Improve Article
Save Article
  • Last Updated : 24 Aug, 2022
Improve Article
Save Article

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 {
 
    // 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);
    }
}


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 {
 
    // 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);
    }
}


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 {
 
    // 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);
    }
}


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 {
 
    // 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);
    }
}


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 {
 
    // 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);
    }
}


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 {
 
    // 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);
    }
}


Output

Queue after insertion: [15, 17, 22]
Queue after deletion: [17, 22]
Queue after deletion: [22]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!