Java Deque & Arraydeque

Java Deque Interface

The Java Deque interface is part of the java.util package and serves as a subtype of the Queue interface. Deque, an abbreviation for double-ended queue, facilitates adding and removing elements from both ends of the data structure.

Hence, a deque is versatile as it can function as a stack or a queue. While a stack adheres to the Last-In-First-Out (LIFO) principle, a queue follows the First-In-First-Out approach. With a deque, it is possible to execute either of these operations. Deque offers a robust abstraction for handling elements in a collection where both ends are open for insertions and deletions.

Deque Interface Declaration

Example

public interface Deque<E> extends Queue<E>

Methods of Java Deque Interface

Method Description
boolean add(object) It is used to insert the specified element into this deque and return true upon success.
boolean offer(object) It is used to insert the specified element into this deque.
Object remove() It is used to retrieve and removes the head of this deque.
Object poll() It is used to retrieve and removes the head of this deque, or returns null if this deque is empty.
Object element() It is used to retrieve, but does not remove, the head of this deque.
Object peek() It is used to retrieve, but does not remove, the head of this deque, or returns null if this deque is empty.
Object peekFirst() The method returns the head element of the deque. The method does not remove any element from the deque. Null is returned by this method, when the deque is empty.
Object peekLast() The method returns the last element of the deque. The method does not remove any element from the deque. Null is returned by this method, when the deque is empty.
Boolean offerFirst(e) Inserts the element e at the front of the queue. If the insertion is successful, true is returned; otherwise, false.
Object offerLast(e) Inserts the element e at the tail of the queue. If the insertion is successful, true is returned; otherwise, false.

Key Features of Deque Interface:

Deque provides support for double-ended operations, enabling the addition and removal of elements from both the front and back of the queue. The available operations are addFirst(E e), addLast(E e), removeFirst, and removeLast.

Accessing Elements: The Deque data structure offers functions that allow accessing elements from both ends without deleting them. These functions are called getFirst and getLast.

Unique Element Manipulation Methods: In addition to standard element insertion and removal operations, Deque provides specific methods like offerFirst(E e) and offerLast(E e) for adding elements, as well as pollFirst and pollLast for deleting elements.

ArrayDeque Class

In Java, it is a known fact that creating an object of an interface is not feasible. To handle instantiation, a class that implements the Deque interface is required, and in this case, that class is ArrayDeque. ArrayDeque is designed to dynamically resize based on its usage patterns. Additionally, it is a subclass of the AbstractCollection class.

The important points about ArrayDeque class are:

  • Unlike Queue, we can add or remove elements from both sides.
  • Null elements are not allowed in the ArrayDeque.
  • ArrayDeque is not thread safe, in the absence of external synchronization.
  • ArrayDeque has no capacity restrictions.
  • ArrayDeque is faster than LinkedList and Stack.
  • ArrayDeque Hierarchy

The diagram on the right side of the page illustrates the structure of the ArrayDeque class hierarchy.

ArrayDeque Class Declaration

Let's examine the declaration of the java.util.ArrayDeque class.

Example

public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable

Java ArrayDeque Example

Example

Example

import java.util.*;

public class Main {

   public static void main(String[] args) {

   //Creating Deque and adding elements

   Deque<String> deque = new ArrayDeque<String>();

   deque.add("Ravi");  

   deque.add("Vijay");   

   deque.add("Ajay");  

   //Traversing elements

   for (String str : deque) {

   System.out.println(str);

   }

   }

}

Output:

Output

Ravi

Vijay

Ajay

Java ArrayDeque Example: offerFirst and pollLast

FileName: DequeExample.java

Example

import java.util.*;

public class Main {

public static void main(String[] args) {

	Deque<String> deque=new ArrayDeque<String>();

	deque.offer("arvind");

	deque.offer("vimal");

	deque.add("mukul");

	deque.offerFirst("jai");

	System.out.println("After offerFirst Traversal...");

	for(String s:deque){

		System.out.println(s);

	}

	//deque.poll();

	//deque.pollFirst();//it is same as poll()

	deque.pollLast();

	System.out.println("After pollLast() Traversal...");

	for(String s:deque){

		System.out.println(s);

	}

}

}

Output:

Output

After offerFirst Traversal...

jai

arvind

vimal

mukul

After pollLast() Traversal...

jai

arvind

vimal

Java ArrayDeque Example: Book

Example

Example

import java.util.*;  

class Book {  

int id;  

String name,author,publisher;  

int quantity;  

public Book(int id, String name, String author, String publisher, int quantity) {  

    this.id = id;  

    this.name = name;  

    this.author = author;  

    this.publisher = publisher;  

    this.quantity = quantity;  

}  

}  

public class Main {  

public static void main(String[] args) {  

	Deque<Book> set=new ArrayDeque<Book>();  

    //Creating Books  

    Book b1=new Book(101,"Let us C","Yashwant Kanetkar","BPB",8);  

    Book b2=new Book(102,"Data Communications & Networking","Forouzan","Mc Graw Hill",4);  

    Book b3=new Book(103,"Operating System","Galvin","Wiley",6);  

    //Adding Books to Deque 

    set.add(b1);  

    set.add(b2);  

    set.add(b3);  

    //Traversing ArrayDeque

    for(Book b:set){  

    System.out.println(b.id+" "+b.name+" "+b.author+" "+b.publisher+" "+b.quantity);  

    }  

}  

}

Output:

Output

101 Let us C Yashwant Kanetkar BPB 8

102 Data Communications & Networking Forouzan Mc Graw Hill 4

103 Operating System Galvin Wiley 6

The Deque interface in Java provides a flexible and effective way to handle collections of elements that need insertion and removal operations at both the beginning and the end. With a comprehensive range of methods, it supports a variety of tasks, making it applicable for diverse use cases. Whether you are working on implementing double-ended queues, stack data structures, or tackling intricate algorithmic challenges, the Deque interface equips you with the essential utilities to enhance your programming workflow.

Input Required

This code uses input(). Please provide values below: