Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Mastering Java's Deque Interface: Features, Methods, and Practical Examples

Java Deque Interface

Discover how Java’s Deque interface delivers a versatile double‑ended queue, and learn how to harness its powerful methods in real code.


How Deque Works

Unlike a conventional queue, which only enqueues at the rear and dequeues from the front, a Deque (double‑ended queue) lets you add and remove elements from both ends. This flexibility makes it ideal for LIFO, FIFO, and more complex data‑handling patterns.

Mastering Java s Deque Interface: Features, Methods, and Practical Examples


Classes Implementing Deque

To work with a Deque, choose one of the following concrete implementations:

Mastering Java s Deque Interface: Features, Methods, and Practical Examples


Using Deque in Your Code

First, import the interface:

import java.util.Deque;

Then instantiate a concrete class:

// Array implementation
Deque<String> animal1 = new ArrayDeque<>();

// LinkedList implementation
Deque<String> animal2 = new LinkedList<>();

These objects now expose all Deque operations.


Key Deque Methods

Because Deque extends Queue, it inherits all queue methods. In addition, it provides specialized operations for both ends:


Deque as a Stack

The legacy Stack class is synchronized and less efficient. Deque offers a modern, unsynchronized alternative:


Practical Example with ArrayDeque

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

class Main {
    public static void main(String[] args) {
        Deque<Integer> numbers = new ArrayDeque<>();

        // Populate the deque
        numbers.offer(1);          // tail
        numbers.offerLast(2);      // tail
        numbers.offerFirst(3);     // head
        System.out.println("Deque: " + numbers);

        // Peek at both ends
        System.out.println("First Element: " + numbers.peekFirst());
        System.out.println("Last Element: " + numbers.peekLast());

        // Remove elements
        System.out.println("Removed First Element: " + numbers.pollFirst());
        System.out.println("Removed Last Element: " + numbers.pollLast());
        System.out.println("Updated Deque: " + numbers);
    }
}

Output

Deque: [3, 1, 2]
First Element: 3
Last Element: 2
Removed First Element: 3
Removed Last Element: 2
Updated Deque: [1]

For deeper insights, explore the official Java ArrayDeque documentation.

Java

  1. Mastering Java Interfaces: Concepts, Implementation, and Best Practices
  2. Java Collections Framework: Core Interfaces, Implementations, and Practical Usage
  3. Java Collection Interface: Core Concepts & Essential Methods
  4. Mastering Java’s Queue Interface: Methods, Implementations, and Practical Use
  5. Mastering Java ArrayDeque: Comprehensive Guide & Stack Implementation
  6. Java Map Interface – Comprehensive Guide to Map, Its Implementations, and Key Methods
  7. Java SortedMap Interface: Overview, Methods, and TreeMap Implementation
  8. Mastering Java NavigableMap: Features, Methods, and TreeMap Implementation
  9. Mastering Java’s ConcurrentMap: Thread‑Safe Maps Explained
  10. Mastering Java’s Set Interface: Concepts, Methods, and Practical Examples