Java Intermediate

0% completed

Previous
Next
ArrayDeque Class

The ArrayDeque class in Java is a resizable array implementation of the Deque interface. It provides a flexible, efficient way to manage elements at both ends of the collection.

Image

ArrayDeque can be used as both a stack (LIFO) and a queue (FIFO), making it a versatile tool for various applications. Unlike some other collections, it does not have capacity restrictions (unless memory is exhausted) and offers constant-time performance for insertion and removal at both ends.

  • Double-Ended Operations: Allows insertion and removal of elements at both the front and back of the deque.
  • Resizable: Automatically grows as needed, without the fixed-size limitation of arrays.
  • Efficient: Provides constant-time performance for most operations like adding, removing, and accessing elements.

Key Characteristics of ArrayDeque

  • Implements Deque Interface: ArrayDeque implements the Deque interface, meaning it supports all operations for double-ended queues.
  • No Capacity Restrictions: Unlike arrays, ArrayDeque can dynamically adjust its capacity.
  • Not Thread-Safe: ArrayDeque is not synchronized. If you need thread-safe operations, consider using Collections.synchronizedDeque() or other concurrent collections.
  • Versatility: Can be used as a stack (with push() and pop()) or as a queue (with offer() and poll()).

Syntax

Basic Syntax to Create an ArrayDeque:

ArrayDeque<Type> deque = new ArrayDeque<Type>();
  • Explanation:
    • ArrayDeque<Type> declares a new ArrayDeque that stores objects of the specified Type.
    • The default constructor creates an empty deque with an initial capacity that expands automatically as elements are added.

Adding Elements:

deque.addFirst(element); // Inserts element at the front. deque.addLast(element); // Inserts element at the back.
  • Explanation:
    • addFirst() adds an element at the beginning of the deque.
    • addLast() adds an element at the end of the deque.

Removing Elements:

Type first = deque.removeFirst(); // Removes and returns the first element. Type last = deque.removeLast(); // Removes and returns the last element.
  • Explanation:
    • removeFirst() removes and returns the front element.
    • removeLast() removes and returns the back element.

Examples

Example 1: Using ArrayDeque as a Queue

In this example, we use ArrayDeque as a standard queue (FIFO). We add elements to the back and remove them from the front.

Java
Java

. . . .

Example Explanation:

  • Queue Behavior:
    • Elements are added to the back of the deque with addLast().
  • Retrieval and Removal:
    • The removeFirst() method retrieves and removes the head element, demonstrating FIFO behavior.
  • Outcome:
    • The output shows the initial queue, the removed element, and the updated queue after the removal.

Example 2: Using ArrayDeque as a Stack

This example demonstrates using ArrayDeque as a stack (LIFO). Elements are added and removed from the front of the deque.

Java
Java

. . . .

Example Explanation :

  • Stack Behavior:
    • Elements are added to the front of the deque using addFirst(), emulating a push operation.
  • Retrieval and Removal:
    • The removeFirst() method removes the top element, acting like a pop operation.
  • Outcome:
    • The output displays the stack before and after the pop, demonstrating LIFO behavior.

ArrayDeque is a versatile and efficient class for managing collections where elements can be added or removed from both ends. By mastering these basic operations, you can effectively use ArrayDeque in a wide range of applications, from task scheduling to implementing stack-based algorithms.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next