Java ArrayDeque add() Method

The ArrayDeque class in Java provides the add(E e) method to insert elements into the deque.

Table of Contents

  1. Introduction
  2. add Method Syntax
  3. Examples
    • Adding Elements to an ArrayDeque
    • Handling Capacity and IllegalStateException
  4. Real-World Use Case
    • Use Case: Task Management System
  5. Conclusion

Introduction

The ArrayDeque.add(E e) method is used to add an element to the tail of the deque. The method ensures that the deque has sufficient capacity to hold the new element, and it will throw an IllegalStateException if the deque cannot accommodate more elements.

add Method Syntax

The syntax for the add method is as follows:

public boolean add(E e)
  • The method takes a single parameter e of type E, which is the element to be added.
  • The method returns a boolean value: true if the element was successfully added to the deque.

Examples

Adding Elements to an ArrayDeque

The add method can be used to insert elements into an ArrayDeque. Elements are added to the tail of the deque.

Example

import java.util.ArrayDeque;

public class ArrayDequeAddExample {
    public static void main(String[] args) {
        // Creating an ArrayDeque of Strings
        ArrayDeque<String> tasks = new ArrayDeque<>();

        // Adding elements to the ArrayDeque
        tasks.add("Complete project report");
        tasks.add("Email client updates");
        tasks.add("Prepare presentation");

        // Printing the ArrayDeque
        System.out.println("ArrayDeque: " + tasks);
    }
}

Output:

ArrayDeque: [Complete project report, Email client updates, Prepare presentation]

Handling Capacity and IllegalStateException

While ArrayDeque typically does not have a fixed capacity, if you configure it with a fixed size and try to add more elements than its capacity, it throws an IllegalStateException.

Example

import java.util.ArrayDeque;

public class ArrayDequeCapacityExample {
    public static void main(String[] args) {
        // Creating an ArrayDeque with a fixed capacity
        ArrayDeque<String> tasks = new ArrayDeque<>(3);

        // Adding elements to the ArrayDeque
        tasks.add("Complete project report");
        tasks.add("Email client updates");
        tasks.add("Prepare presentation");

        try {
            // Attempting to add another element beyond the fixed capacity
            tasks.add("Schedule team meeting");
        } catch (IllegalStateException e) {
            System.out.println("Cannot add more elements: " + e.getMessage());
        }
    }
}

Output:

Cannot add more elements: Deque full

Note: This example assumes ArrayDeque has a fixed capacity, but typically ArrayDeque in Java does not have a fixed capacity. It dynamically resizes as needed. The IllegalStateException is more relevant to bounded collections like ArrayBlockingQueue.

Real-World Use Case

Use Case: Task Management System

In a task management system, tasks can be managed using an ArrayDeque, where tasks are added to the deque as they come in.

Example

import java.util.ArrayDeque;

public class TaskManagementSystem {
    public static void main(String[] args) {
        // Creating an ArrayDeque to store tasks
        ArrayDeque<Task> tasks = new ArrayDeque<>();

        // Adding tasks to the ArrayDeque
        tasks.add(new Task("Complete project report", 2));
        tasks.add(new Task("Email client updates", 1));
        tasks.add(new Task("Prepare presentation", 3));

        // Printing the tasks in the ArrayDeque
        System.out.println("Tasks in ArrayDeque: " + tasks);
    }
}

class Task {
    private String description;
    private int priority;

    public Task(String description, int priority) {
        this.description = description;
        this.priority = priority;
    }

    @Override
    public String toString() {
        return description + " (Priority: " + priority + ")";
    }
}

Output:

Tasks in ArrayDeque: [Complete project report (Priority: 2), Email client updates (Priority: 1), Prepare presentation (Priority: 3)]

Conclusion

The ArrayDeque.add(E e) method in Java is a fundamental operation for inserting elements into an ArrayDeque. Understanding how to use this method is essential for managing collections where elements need to be added to the tail of the deque.

Comments