Loading

Quipoin Menu

Learn • Practice • Grow

data-structure-with-java / Deque Introduction
tutorial

Deque Introduction

A deque (double-ended queue) is a generalized queue where you can insert and delete elements from both ends. It stands for "double-ended queue".

Deque supports operations:
  • addFirst / offerFirst – insert at front
  • addLast / offerLast – insert at rear
  • removeFirst / pollFirst – remove from front
  • removeLast / pollLast – remove from rear
  • getFirst / peekFirst – examine front
  • getLast / peekLast – examine rear
In Java, the `Deque` interface is part of the collections framework. `ArrayDeque` provides a resizable-array implementation and is usually faster than `LinkedList` for deque operations.

Example usage:


Deque deque = new ArrayDeque<>();
deque.addFirst(1); // front: 1
deque.addLast(2); // front: 1, rear: 2
deque.addFirst(0); // front: 0, then 1, then 2
System.out.println(deque.removeLast()); // 2
System.out.println(deque.removeFirst()); // 0
Two Minute Drill
  • Deque allows insertion/removal from both ends.
  • Supports both stack and queue operations.
  • Java provides `ArrayDeque` and `LinkedList` implementations.
  • Useful for sliding window problems, palindrome checking, etc.

Need more clarification?

Drop us an email at career@quipoinfotech.com