GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • ArrayDeque:
  • LinkedList:
  • ConcurrentLinkedDeque:
  • Introduction3 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Deque Interface in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Deque Interface in Java

Last Updated : 24 Apr, 2026

The Deque interface is part of the java.util package and extends the Queue interface. It stands for Double-Ended Queue and represents a linear collection that allows insertion, removal, and retrieval of elements from both ends.

  • Allows insertion and deletion from both front and rear
  • Can be used as both stack (LIFO) and queue (FIFO)
  • Common implementations are ArrayDeque and LinkedList

Declaration

public interface Deque<E> extends Queue<E>

Here, E is the type of elements stored in the deque.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class Geeks 
{
    public static void main(String[] args)
    {

        // Create a Deque of Strings
        Deque<String> d = new ArrayDeque<>();

        d.addFirst("1");
        d.addLast("2");

        String f = d.removeFirst();
        String l = d.removeLast();

        // Displaying the Deque
        System.out.println("First: " + f + ", Last: " + l);
    }
}

Output
First: 1, Last: 2

Hierarchy of Deque interface

It extends the Queue interface which extends the Collection Interface. It has implementation classes like ArrayDeque and LinkedList.

iterable
Deque

Common Implementations classes

  • ArrayDeque: Resizable array implementation; fast for insertion/removal at both ends; not thread-safe.
  • LinkedList: Doubly-linked list; supports all deque operations; allows nulls.
  • ConcurrentLinkedDeque: Thread-safe deque based on linked nodes; suitable for concurrent use.

Deque Operations using ArrayDeque

Let’s see how to perform a few frequently used operations on the Deque using the ArrayDeque class. 

1. Adding Elements

To add elements in a Deque, you can use add() , addFirst(), or addLast(), allowing insertion at either end, unlike a standard Queue which only adds at the tail.

Java
import java.util.*;
public class ArrayDequeDemo {
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque<String> dq = new ArrayDeque<String>();

        // add() method to insert
        dq.add("For");
        dq.addFirst("Geeks");
        dq.addLast("Geeks");

        System.out.println(dq);
    }
}

Output
[Geeks, For, Geeks]

2. Removing Elements

Elements can be removed from both ends using removeFirst(), removeLast(), pop(), or poll() variants; pop() throws an exception if empty, while poll() is safe. 

Java
import java.util.*;

public class ArrayDequeDemo {
  
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque<String> dq = new ArrayDeque<String>();

        // add() method to insert
        dq.add("For");
        dq.addFirst("Geeks");
        dq.addLast("Geeks");

        System.out.println(dq);
      
        System.out.println(dq.pop());
      
        System.out.println(dq.poll());
      
        System.out.println(dq.pollFirst());
      
        System.out.println(dq.pollLast());
    }
}

Output
[Geeks, For, Geeks]
Geeks
For
Geeks
null

3. Iterating through the Deque

Since a Deque can be iterated from both directions, the iterator method of the Deque interface provides us two ways to iterate. One from the first and the other from the back. 

Java
import java.util.*;

public class ArrayDequeDemo {
    public static void main(String[] args)
    {
        // Initialize Deque
        Deque<String> dq = new ArrayDeque<>();
        dq.add("For");
        dq.addFirst("Geeks");
        dq.addLast("Geeks");
        dq.add("is so good");

        // Type-safe forward iteration
        for (Iterator<String> itr = dq.iterator();
             itr.hasNext();) {
            System.out.print(itr.next() + " ");
        }
        System.out.println();

        // Type-safe reverse iteration
        for (Iterator<String> itr = dq.descendingIterator();
             itr.hasNext();) {
            System.out.print(itr.next() + " ");
        }
    }
}

Output
Geeks For Geeks is so good 
is so good Geeks For Geeks 

Methods of Deque Interface

Below are some common list of methods of Deque Interface

MethodDescription
addFirst(E e)Inserts element at the front (head).
addLast(E e)Inserts element at the end (tail).
offerFirst(E e)Adds element at head; returns false if unable.
offerLast(E e)Adds element at tail; returns false if unable.
add(E e) Adds element at the tail.

offer(E e)

Adds element at the tail.

removeFirst()Removes and returns first element; throws exception if empty.
removeLast()Removes and returns last element; throws exception if empty.
pollFirst()Removes and returns first element; returns null if empty.
pollLast()Removes and returns last element; returns null if empty.
poll()Removes and returns head safely; returns null if empty.
getFirst()Retrieves first element without removing; throws exception if empty.
getLast()Retrieves last element without removing; throws exception if empty.
peekFirst()Retrieves first element safely; returns null if empty.
peekLast()Retrieves last element safely; returns null if empty.
peek()Retrieves head element safely; returns null if empty.
push(E e)Inserts element at the head (top of stack).
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.