Deque in Java

In Java, Deque stands for “double-ended queue,” which is a data structure that enables the addition and removal of elements from either end of the queue. This data structure has an ordered sequence of elements that can be accessed using a variety of methods. This article will explain what a Deque is, how to create one in Java, and how to use its various methods to manipulate data.

What is a Deque?


A Deque is a data structure that enables the addition and removal of elements from either end of the queue. It is similar to a stack and a queue, but it provides more flexibility than either. It is implemented as an interface in Java, which means that it defines a set of methods that a class must implement. These methods enable the addition and removal of elements from the front and back of the Deque. A Deque can also be used as a stack or a queue, depending on the needs of the program.

Creating a Deque in Java


To create a Deque in Java, we need to import the java.util.Deque package. This package contains the Deque interface, which defines the methods that must be implemented. We can create a Deque using one of the following classes: ArrayDeque or LinkedList. Both of these classes implement the Deque interface.

Creating a Deque using ArrayDeque


The ArrayDeque class is implemented as an array of objects. It provides constant time access to the front and back of the Deque. Here is an example of how to create an ArrayDeque:

Deque deque = new ArrayDeque();

Creating a Deque using LinkedList


The LinkedList class is implemented as a linked list of objects. It provides constant time access to the front and back of the Deque. Here is an example of how to create a LinkedList:

Deque deque = new LinkedList();

Deque Methods


The Deque interface defines several methods that enable the addition and removal of elements from the front and back of the Deque. Here are some of the most commonly used methods:

  1. addFirst(E e) – adds the specified element to the front of the Deque
  2. addLast(E e) – adds the specified element to the back of the Deque
  3. offerFirst(E e) – adds the specified element to the front of the Deque, returning true if the element was added successfully
  4. offerLast(E e) – adds the specified element to the back of the Deque, returning true if the element was added successfully
  5. removeFirst() – removes and returns the first element of the Deque
  6. removeLast() – removes and returns the last element of the Deque
  7. pollFirst() – removes and returns the first element of the Deque, returning null if the Deque is empty
  8. pollLast() – removes and returns the last element of the Deque, returning null if the Deque is empty
  9. getFirst() – returns the first element of the Deque without removing it
  10. getLast() – returns the last element of the Deque without removing it
  11. peekFirst() – returns the first element of the Deque without removing it, returning null if the Deque is empty
  12. peekLast() – returns the last element of the Deque without removing it, returning null if the Deque is empty
  13. size() – returns the number of elements in the Deque

Example Usage


Here is an example of how to use a Deque in Java:

Deque deque = new ArrayDeque();
deque.addLast(1);
deque.addLast(2);
deque.addLast(3);
System.out.println(deque); // prints [1, 2, 3]
deque.addFirst(4);
System.out.println(deque); // prints [4, 1, 2, 3]
deque.removeLast();
System.out.println(deque); // prints [4, 1, 2]
System.out.println(deque.getFirst()); // prints 4
System.out.println(deque.getLast()); // prints 2
System.out.println(deque.peekFirst()); // prints 4
System.out.println(deque.peekLast()); // prints 2
System.out.println(deque.size()); // prints 3

In this example, we created an ArrayDeque and added three elements to the back of the Deque using the addLast method. We then printed the contents of the Deque using the toString method. Next, we added an element to the front of the Deque using the addFirst method and printed the contents of the Deque again. We then removed the last element from the Deque using the removeLast method and printed the contents of the Deque again. Finally, we printed the first and last elements of the Deque using the getFirst and getLast methods, respectively. We also printed the first and last elements of the Deque using the peekFirst and peekLast methods, which do not remove the elements from the Deque. Finally, we printed the size of the Deque using the size method.

Conclusion


Deques are a useful data structure in Java that provide the flexibility of both stacks and queues. They can be implemented using either an ArrayDeque or a LinkedList. The Deque interface defines several methods that enable the addition and removal of elements from the front and back of the Deque. Deques can be used in a variety of applications, including algorithms that require the ability to add and remove elements from either end of a collection.