Java ArrayDeque addLast() Method

The ArrayDeque class in Java provides the addLast(E e) method to insert an element at the end of the deque.

Table of Contents

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

Introduction

The ArrayDeque.addLast(E e) method is used to add an element to the end of the deque. This 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.

addLast Method Syntax

The syntax for the addLast method is as follows:

public void addLast(E e)
  • The method takes a single parameter e of type E, which is the element to be added to the end of the deque.
  • The method does not return any value.

Examples

Adding Elements to the End of an ArrayDeque

The addLast method can be used to insert elements at the end of an ArrayDeque.

Example

import java.util.ArrayDeque;

public class ArrayDequeAddLastExample {
    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");

        // Adding an element to the end of the ArrayDeque
        tasks.addLast("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");

        // Adding an element to the end of the ArrayDeque
        tasks.addLast("Prepare presentation");

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

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

Output:

ArrayDeque: [Complete project report, Email client updates, Prepare presentation]
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 added to the end of the deque using the addLast method, ensuring they are processed in the order they were added.

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));

        // Adding a low-priority task to the end of the ArrayDeque
        tasks.addLast(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.addLast(E e) method in Java is a fundamental operation for inserting elements at the end of an ArrayDeque. Understanding how to use this method is essential for managing collections where elements need to be added to the end of the deque.

Comments