How to use ArrayDeque as Stack and Queue in Java

May 04, 2019 | No comments

ArrayDeque in Java

The name deque is short for "double ended queue" and is usually pronounced "deck". ArrayDeque is an implementation of Deque interface. ArrayDeque is array based implementation of Deque. In this article we will walk though using ArrayDeque. Since Deque supports adding and removing objects from both ends, it can be used as both Stack(LIFO) and Queue(FIFO) data structure. Deque interface extends Queue interface.

ArrayDeque As Stack

Deque can be used as a stack. Following are the methods that can be used to have the stack (LIFO) behaviour. These methods are defined in the Deque interface and implemented by ArrayDeque. push : to add object at the top of the stack.
peek: to see what is at the top of the stack, without removing it.
pop: to remove the top of the stack.
Deque as Stack Example
Following code shows how to use Deque as a Stack.
Deque 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.addFirst("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("Peek top of the stack = " + stack.pop()); 

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

//Print entries of the Stack Using Iterator
System.out.println("Entries in the Stack Using Iterator");
Iterator it2 = stack.iterator();
while(it2.hasNext()) {
  String item = it2.next();
  System.out.println(item);
}
Following image shows the content of the stack after pushing all the items.

ArrayDeque as Queue

These methods are defined in the Queue interface and implemented by ArrayDeque.
offer: adds item to the queue.
peek: retrieve the head of the queue.
poll: remove the head of the queue.
As Queue
Following example shows using ArrayDeque as a queue.
Queue 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 top of the queue
System.out.println("Peek top of the queue = " + queue.peek());
Assert.assertEquals("Should be the first object offered to queue", "Transformers (2007)",
    queue.peek());


// Peek the top of the queue
System.out.println("Poll top of the queue = ");
Assert.assertEquals("Should be the first object offered to queue", "Transformers (2007)",
    queue.poll());

// Print entries of the queue
System.out.println("Entries in the queue");
queue.forEach(a -> System.out.println(a));

// Print entries of the Stack Using Iterator
System.out.println("Entries in the queue Using Iterator");
  Iterator it2 = queue.iterator();
  while (it2.hasNext()) {
    String item = it2.next();
    System.out.println(item);
  }
}
Following image shows the content of the queue after adding all the items to the queue.
Summary
  • ArrayDeque is array based implementation of Deque interface.
  • It can be used both as a Stack (LIFO) or Queue(FIFO) data structure.
  • It does not accept Null Objects.
  • An Iterator returned by an ArrayDeque is fail-fast.

Full Example

ArrayDequeExample.java
package bootng.java.collections;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.Queue;
import org.junit.Assert;

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<String>();

    // 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());
    Assert.assertEquals("Should be the last object pushed to stack", "Bumblebee (2018)",
        stack.peek());

    // Pop the top of the stack
    System.out.println("Pop top of the stack = ");
    Assert.assertEquals("Should be the last object pushed to stack", "Bumblebee (2018)",
        stack.pop());

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

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


  }

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

    // push items to stack
    queue.offer("Transformers (2007)"); // offer == addLast
    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 top of the queue
    System.out.println("Peek top of the queue = " + queue.peek());
    Assert.assertEquals("Should be the first object offered to queue", "Transformers (2007)",
        queue.peek());


    // Peek the top of the queue
    System.out.println("Poll top of the queue = ");
    Assert.assertEquals("Should be the first object offered to queue", "Transformers (2007)",
        queue.poll());

    // Print entries of the queue
    System.out.println("Entries in the queue");
    queue.forEach(a -> System.out.println(a));

    // Print entries of the Stack 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);
    }
  }
}

No comments :

Post a Comment

Please leave your message queries or suggetions.