How to use ArrayDeque as Stack and Queue in Java

How to use ArrayDeque as Stack and Queue in Java

The name Deque is short for "double ended queue" and is usually pronounced "deck". ArrayDeque is an implementation of the Deque interface. Since Deque supports adding and removing objects from both ends, it can be used as both Stack (LIFO) and Queue (FIFO). The Deque interface extends Queue.

ArrayDeque As Stack

Example 1
Following code shows how to use Deque as a Stack. We use the push method to add items on top of the stack, resulting in the last item being taken out (LIFO).
Deque<String> stack = new ArrayDeque<>();

// push items to stack
stack.push("Transformers (2007)");
stack.push("Transformers: Revenge of the Fallen (2009)");
stack.push("Transformers: Dark of the Moon (2011)");
stack.push("Transformers: Age of Extinction (2014)");
stack.push("Transformers: The Last Knight (2017)");
stack.push("Bumblebee (2018)");

// Size of the stack
System.out.println("Size of the stack = " + stack.size());

// Peek the top of the stack
System.out.println("Peek top of the stack = " + stack.peek());

// Pop the top of the stack
System.out.println("Pop top of the stack = " + stack.pop());

// Print entries of the Stack
System.out.println("Entries in the Stack");
stack.forEach(System.out::println);

// Print entries of the Stack Using Iterator
System.out.println("Entries in the Stack Using Iterator");
Iterator<String> it2 = stack.iterator();
while (it2.hasNext()) {
  String item = it2.next();
  System.out.println(item);
}

ArrayDeque as Queue

Example 2
Following example shows using ArrayDeque as a queue. We use the offer method to add items to the end of the list, making it FIFO.
Queue<String> queue = new ArrayDeque<>();

// add items to queue
queue.offer("Transformers (2007)");
queue.offer("Transformers: Revenge of the Fallen (2009)");
queue.offer("Transformers: Dark of the Moon (2011)");
queue.offer("Transformers: Age of Extinction (2014)");
queue.offer("Transformers: The Last Knight (2017)");
queue.offer("Bumblebee (2018)");

// Size of the queue
System.out.println("Size of the queue = " + queue.size());

// Peek the front of the queue
System.out.println("Peek front of the queue = " + queue.peek());

// Poll the front of the queue
System.out.println("Poll front of the queue = " + queue.poll());

// Print entries of the queue
System.out.println("Entries in the Queue");
queue.forEach(System.out::println);

// Print entries of the Queue Using Iterator
System.out.println("Entries in the Queue Using Iterator");
Iterator<String> it2 = queue.iterator();
while (it2.hasNext()) {
  String item = it2.next();
  System.out.println(item);
}
Full Example ArrayDequeExample.java
Following code contains the full class ArrayDequeExample
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Queue;

public class ArrayDequeExample {

  public static void main(String[] args) {
    ArrayDequeExample example = new ArrayDequeExample();

    example.asStack();
    example.asQueue();
  }

  private void asStack() {
    System.out.println(" ==========================");
    Deque<String> stack = new ArrayDeque<>();

    stack.push("Transformers (2007)");
    stack.push("Transformers: Revenge of the Fallen (2009)");
    stack.push("Transformers: Dark of the Moon (2011)");
    stack.push("Transformers: Age of Extinction (2014)");
    stack.push("Transformers: The Last Knight (2017)");
    stack.push("Bumblebee (2018)");

    System.out.println("Size of the stack = " + stack.size());
    System.out.println("Peek top of the stack = " + stack.peek());
    System.out.println("Pop top of the stack = " + stack.pop());

    System.out.println("Entries in the Stack");
    stack.forEach(System.out::println);

    System.out.println("Entries in the Stack Using Iterator");
    Iterator<String> it2 = stack.iterator();
    while (it2.hasNext()) {
      System.out.println(it2.next());
    }
  }

  private void asQueue() {
    System.out.println(" ==========================");
    Queue<String> queue = new ArrayDeque<>();

    queue.offer("Transformers (2007)");
    queue.offer("Transformers: Revenge of the Fallen (2009)");
    queue.offer("Transformers: Dark of the Moon (2011)");
    queue.offer("Transformers: Age of Extinction (2014)");
    queue.offer("Transformers: The Last Knight (2017)");
    queue.offer("Bumblebee (2018)");

    System.out.println("Size of the queue = " + queue.size());
    System.out.println("Peek front of the queue = " + queue.peek());
    System.out.println("Poll front of the queue = " + queue.poll());

    System.out.println("Entries in the Queue");
    queue.forEach(System.out::println);

    System.out.println("Entries in the Queue Using Iterator");
    Iterator<String> it2 = queue.iterator();
    while (it2.hasNext()) {
      System.out.println(it2.next());
    }
  }
}

Summary

  • Stack is a data structure that follows LIFO (Last In, First Out).
  • Queue is a data structure that follows FIFO (First In, First Out).
  • Deque allows adding and removing items from both ends.
  • Deque<E> is an interface in Java, with ArrayDeque and LinkedList as common implementations.

No comments :

Post a Comment

Please leave your message queries or suggetions.

Note: Only a member of this blog may post a comment.