The ArrayDeque
class in Java provides the addFirst(E e)
method to insert an element at the front of the deque.
Table of Contents
- Introduction
addFirst
Method Syntax- Examples
- Adding Elements to the Front of an ArrayDeque
- Handling Capacity and IllegalStateException
- Real-World Use Case
- Use Case: Task Management System
- Conclusion
Introduction
The ArrayDeque.addFirst(E e)
method is used to add an element to the front 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.
addFirst Method Syntax
The syntax for the addFirst
method is as follows:
public void addFirst(E e)
- The method takes a single parameter
e
of typeE
, which is the element to be added to the front of the deque. - The method does not return any value.
Examples
Adding Elements to the Front of an ArrayDeque
The addFirst
method can be used to insert elements at the front of an ArrayDeque
.
Example
import java.util.ArrayDeque;
public class ArrayDequeAddFirstExample {
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 front of the ArrayDeque
tasks.addFirst("Prepare presentation");
// Printing the ArrayDeque
System.out.println("ArrayDeque: " + tasks);
}
}
Output:
ArrayDeque: [Prepare presentation, Complete project report, Email client updates]
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 front of the ArrayDeque
tasks.addFirst("Prepare presentation");
try {
// Attempting to add another element beyond the fixed capacity
tasks.addFirst("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: [Prepare presentation, Complete project report, Email client updates]
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, high-priority tasks can be added to the front of the deque using the addFirst
method, ensuring they are processed first.
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 high-priority task to the front of the ArrayDeque
tasks.addFirst(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: [Prepare presentation (Priority: 3), Complete project report (Priority: 2), Email client updates (Priority: 1)]
Conclusion
The ArrayDeque.addFirst(E e)
method in Java is a fundamental operation for inserting elements at the front of an ArrayDeque
. Understanding how to use this method is essential for managing collections where elements need to be prioritized and added to the front of the deque.
Comments
Post a Comment
Leave Comment