0
0
JavaHow-ToBeginner · 4 min read

How to Use Deque in Java: Syntax, Example, and Tips

In Java, Deque is a double-ended queue interface that allows adding and removing elements from both ends. You can use classes like ArrayDeque or LinkedList to implement it and perform operations such as addFirst(), addLast(), removeFirst(), and removeLast().
📐

Syntax

The Deque interface in Java is part of the java.util package and supports operations at both ends of the queue.

Common methods include:

  • addFirst(E e): Adds element at the front.
  • addLast(E e): Adds element at the end.
  • removeFirst(): Removes and returns the first element.
  • removeLast(): Removes and returns the last element.
  • peekFirst(): Views the first element without removing.
  • peekLast(): Views the last element without removing.

Implementations like ArrayDeque or LinkedList provide concrete classes.

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

Deque<String> deque = new ArrayDeque<>();
deque.addFirst("front");
deque.addLast("end");
String first = deque.removeFirst();
String last = deque.removeLast();
💻

Example

This example shows how to create a Deque, add elements to both ends, and remove them while printing the results.

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

public class DequeExample {
    public static void main(String[] args) {
        Deque<String> deque = new ArrayDeque<>();

        deque.addFirst("Apple");
        deque.addLast("Banana");
        deque.addFirst("Cherry");

        System.out.println("Deque after additions: " + deque);

        String first = deque.removeFirst();
        System.out.println("Removed from front: " + first);

        String last = deque.removeLast();
        System.out.println("Removed from end: " + last);

        System.out.println("Deque now: " + deque);
    }
}
Output
Deque after additions: [Cherry, Apple, Banana] Removed from front: Cherry Removed from end: Banana Deque now: [Apple]
⚠️

Common Pitfalls

Some common mistakes when using Deque include:

  • Using removeFirst() or removeLast() on an empty deque causes NoSuchElementException. Use pollFirst() or pollLast() to avoid exceptions; they return null if empty.
  • Confusing addFirst() with offerFirst(). The add methods throw exceptions if capacity is limited and full, while offer methods return false.
  • Using LinkedList as a deque is fine but ArrayDeque is usually faster and preferred unless you need null elements.
java
import java.util.Deque;
import java.util.ArrayDeque;

public class DequePitfall {
    public static void main(String[] args) {
        Deque<String> deque = new ArrayDeque<>();

        // Wrong: removeFirst() on empty deque throws exception
        // String first = deque.removeFirst(); // Throws NoSuchElementException

        // Right: pollFirst() returns null if empty
        String firstSafe = deque.pollFirst();
        System.out.println("Safe removal from empty deque returns: " + firstSafe);
    }
}
Output
Safe removal from empty deque returns: null
📊

Quick Reference

MethodDescription
addFirst(E e)Add element at the front, throws exception if full
addLast(E e)Add element at the end, throws exception if full
offerFirst(E e)Add element at the front, returns false if full
offerLast(E e)Add element at the end, returns false if full
removeFirst()Remove and return first element, throws exception if empty
removeLast()Remove and return last element, throws exception if empty
pollFirst()Remove and return first element, returns null if empty
pollLast()Remove and return last element, returns null if empty
peekFirst()View first element without removing, returns null if empty
peekLast()View last element without removing, returns null if empty

Key Takeaways

Use Deque to add or remove elements efficiently from both ends of a queue.
Prefer ArrayDeque for better performance unless you need null elements or thread safety.
Avoid exceptions by using pollFirst() and pollLast() instead of removeFirst() and removeLast() on empty deques.
Remember add methods throw exceptions if capacity is limited and full, while offer methods return false.
Use peekFirst() and peekLast() to safely view elements without removing them.