English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java other topics

Java Deque Interface

In this tutorial, we will learn about the Deque interface, how to use it, and its methods.

The Deque interface of the Java Collection Framework provides the functionality of a deque (Deque). It inherits from the Queue interface.

How deque works

In a regular queue, elements are added from the back and removed from the front. However, in a deque, we canInsert and delete elements from both ends.

Classes that implement Deque

To use the functions of the Deque interface, we need to use classes that implement the interface:

How to use Deque?

In Java, we must import the package java.util.Deque to use Deque.

Deque<String> animal1 = new ArrayDeque<>();
Deque<String> animal2 = new LinkedList<>();

In this case, we create objects of the class ArrayDeque and LinkedList as animal1and animal2. These objects can use the functions of the Deque interface.

Deque methods

Since Deque inherits the Queue interface, it inheritsof the Queue interfaceAll methods.

In addition to the methods available in the Queue interface, the Deque interface also includes the following methods:

  • addFirst() - Add the specified element to the beginning of the deque. If the deque is full, raise an exception.

  • addLast() - Add the specified element to the end of the deque. If the deque is full, raise an exception.

  • offerFirst() - Add the specified element to the beginning of the deque. If the deque is full, return false.

  • offerLast() - Add the specified element to the end of the deque. If the deque is full, return false.

  • getFirst() - Return the first element of the deque. If the deque is empty, throw an exception.

  • getLast() - Return the last element of the deque. If the deque is empty, throw an exception.

  • peekFirst() - Return the first element of the deque. If the deque is empty, return null.

  • peekLast() - Return the last element of the deque. If the deque is empty, return null.

  • removeFirst() - Return and delete the first element of the deque. If the deque is empty, throw an exception.

  • removeLast() - Return and delete the last element of the deque. If the deque is empty, throw an exception.

  • pollFirst() - Return and delete the first element of the deque. If the deque is empty, return null.

  • pollLast() - Return and delete the last element of the deque. If the deque is empty, return null.

Deque as a stack data structure

The Java Collections framework provides an implementation of the stack through the Stack class.

However, it is recommended to use Deque as a stack rather thanStack class.This is because the Stack methods are synchronized.

The following are the methods provided by the Deque interface for implementing a stack:

  • push() - Add an element to the front of the deque

  • pop() - Delete an element from the front of the deque

  • peek() - Return an element from the front of the deque

Implementation of Deque in ArrayDeque class

import java.util.Deque;
import java.util.ArrayDeque;
class Main {
    public static void main(String[] args) {
        // Create Deque using ArrayDeque class 
        Deque<Integer> numbers = new ArrayDeque<>();
        //Add elements to Deque
        numbers.offer(1);
        numbers.offerLast(2);
        numbers.offerFirst(3);
        System.out.println("Deque: "	 + numbers);
        //Access elements of Deque
        int	firstElement	=	numbers.peekFirst();
        System.out.println("The first element: "	 + firstElement);
        int lastElement = numbers.peekLast();
        System.out.println("Last element: " + lastElement);
        //Remove elements from Deque
        int removedNumber1 = numbers.pollFirst();
        System.out.println("Remove the first element: " + removedNumber1);
        int removedNumber2 = numbers.pollLast();
        System.out.println("Remove the last element: " + removedNumber2);
        System.out.println("Updated Deque: " + numbers);
    }
}

Output Result

Deque: [3, 1, 2]
First element: 3
Last element: 2
Remove the first element: 3
Remove the last element: 2
Updated Deque: [1]

For more information, please visitJava ArrayDeque.