Java ArrayDeque

Introduction

The ArrayDeque class in Java, part of the java.util package, is a resizable array implementation of the Deque interface. It provides a double-ended queue, allowing elements to be added or removed from both ends.

Table of Contents

  1. What is the ArrayDeque Class?
  2. Features of ArrayDeque
  3. Common Methods
  4. Examples of Using ArrayDeque
  5. Conclusion

1. What is the ArrayDeque Class?

ArrayDeque is a collection that supports element insertion and removal at both ends, offering an efficient alternative to linked lists and other deque implementations.

2. Features of ArrayDeque

  • No capacity restrictions (dynamically resizable).
  • Faster than LinkedList for queue operations.
  • Not thread-safe (requires external synchronization for concurrent use).

3. Common Methods

  • addFirst(E e): Inserts the specified element at the front.
  • addLast(E e): Inserts the specified element at the end.
  • offerFirst(E e): Inserts the specified element at the front without throwing an exception.
  • offerLast(E e): Inserts the specified element at the end without throwing an exception.
  • removeFirst(): Removes and returns the first element.
  • removeLast(): Removes and returns the last element.
  • pollFirst(): Retrieves and removes the first element, or returns null if empty.
  • pollLast(): Retrieves and removes the last element, or returns null if empty.
  • getFirst(): Retrieves, but does not remove, the first element.
  • getLast(): Retrieves, but does not remove, the last element.
  • peekFirst(): Retrieves, but does not remove, the first element, or returns null.
  • peekLast(): Retrieves, but does not remove, the last element, or returns null.
  • size(): Returns the number of elements in the deque.

4. Examples of Using ArrayDeque

Example 1: Basic Operations

This example demonstrates basic operations such as adding, removing, and accessing elements.

import java.util.ArrayDeque;

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

        // Adding elements
        deque.addFirst("First");
        deque.addLast("Last");
        System.out.println("Deque after additions: " + deque);

        // Accessing elements
        System.out.println("First element: " + deque.getFirst());
        System.out.println("Last element: " + deque.getLast());

        // Removing elements
        deque.removeFirst();
        deque.removeLast();
        System.out.println("Deque after removals: " + deque);
    }
}

Output:

Deque after additions: [First, Last]
First element: First
Last element: Last
Deque after removals: []

Example 2: Using offer and poll

This example shows the use of offer and poll methods for safe element insertion and removal.

import java.util.ArrayDeque;

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

        // Offering elements
        deque.offerFirst(1);
        deque.offerLast(2);
        System.out.println("Deque after offers: " + deque);

        // Polling elements
        System.out.println("Polled first: " + deque.pollFirst());
        System.out.println("Polled last: " + deque.pollLast());
        System.out.println("Deque after polls: " + deque);
    }
}

Output:

Deque after offers: [1, 2]
Polled first: 1
Polled last: 2
Deque after polls: []

Example 3: Peeking at Elements

This example demonstrates how to peek at elements without removing them.

import java.util.ArrayDeque;

public class PeekExample {
    public static void main(String[] args) {
        ArrayDeque<String> deque = new ArrayDeque<>();
        deque.add("Element");

        System.out.println("Peek first: " + deque.peekFirst());
        System.out.println("Peek last: " + deque.peekLast());
    }
}

Output:

Peek first: Element
Peek last: Element

5. Conclusion

The ArrayDeque class in Java is a versatile and efficient implementation of the Deque interface. It offers fast operations for both ends of the queue, making it a suitable choice for many applications that require double-ended queue functionality.

Comments