ArrayDeque (Array Double-Ended Queue) is a resizable array implementation of the Deque interface. It is part of the Java Collections Framework and resides in the java.util package.
Key Points:
Resizable Array: ArrayDeque uses a resizable array, which means it can grow as needed.
Null Values: Unlike some other collections like PriorityQueue, ArrayDeque doesn’t allow null elements.
Performance: Offers better performance than LinkedList for both stack and queue operations since it’s an array-based data structure. The insertion, removal, and access operations run in constant time (amortized).
Not Thread-Safe: It’s not synchronized, meaning that if it’s used concurrently by multiple threads, and at least one of the threads modifies it, it must be synchronized externally.
Double-Ended: Elements can be added/removed at both ends.
ArrayDeque Common Operations
import java.util.ArrayDeque;
public class ArrayDequeDemo {
public static void main(String[] args) {
// 1. Initialization
ArrayDeque<Integer> deque = new ArrayDeque<>();
System.out.println("Initialized deque: " + deque);
// 2. Adding elements
deque.add(10); // adds at the end
deque.addFirst(5); // adds at the beginning
deque.addLast(15); // adds at the end
System.out.println("After adding elements: " + deque);
// 3. Peek elements (without removal)
System.out.println("First element (without removal): " + deque.peekFirst());
System.out.println("Last element (without removal): " + deque.peekLast());
// 4. Removing elements
deque.removeFirst(); // removes the first element
deque.removeLast(); // removes the last element
System.out.println("After removing elements: " + deque);
}
}
Output:
Initialized deque: [] After adding elements: [5, 10, 15] First element (without removal): 5 Last element (without removal): 15 After removing elements: [10]
Explanation:
1. An empty ArrayDeque of type Integer is created.
2.add(10) adds the integer 10 to the end of the deque.
3. addFirst(5) adds the integer 5 to the beginning of the deque.
4. addLast(15) adds the integer 15 to the end of the deque.
5. peekFirst() retrieves the first element without removing it from the deque.
6. peekLast() retrieves the last element without removing it from the deque.
7. removeFirst() removes and returns the first element from the deque.
8. removeLast() removes and returns the last element from the deque.
Employee Task Management with ArrayDeque as Stack
import java.util.ArrayDeque;
class Employee {
private String name;
private String task;
public Employee(String name, String task) {
this.name = name;
this.task = task;
}
@Override
public String toString() {
return name + " assigned: " + task;
}
}
public class EmployeeManagement {
public static void main(String[] args) {
// 1. Initialization
ArrayDeque<Employee> taskStack = new ArrayDeque<>();
System.out.println("Initialized task stack: " + taskStack);
// 2. Assigning tasks
taskStack.push(new Employee("John", "Write the weekly report"));
taskStack.push(new Employee("Jane", "Attend client meeting"));
taskStack.push(new Employee("Bob", "Review code changes"));
System.out.println("\nAfter assigning tasks:");
for (Employee e : taskStack) {
System.out.println(e);
}
// 3. Completing tasks
System.out.println("\n" + taskStack.pop() + " - Task Completed!");
System.out.println(taskStack.peek() + " - Next Task to Complete!");
// 4. Status after completing a task
System.out.println("\nStatus after completing a task:");
for (Employee e : taskStack) {
System.out.println(e);
}
}
}
Output:
Initialized task stack: [] After assigning tasks: Bob assigned: Review code changes Jane assigned: Attend client meeting John assigned: Write the weekly report Bob assigned: Review code changes - Task Completed! Jane assigned: Attend client meeting - Next Task to Complete! Status after completing a task: Jane assigned: Attend client meeting John assigned: Write the weekly report
Explanation:
1. An ArrayDeque of type Employee is initialized to manage tasks. ArrayDeque can be efficiently used as a stack.
2. Tasks are assigned to employees. They are added to the stack with the push operation, so the last task added is on the top.
3. pop() is used to mark the completion of the most recently assigned task.
4. peek() is used to check the next task that needs to be completed without removing it.
5. The status of the task stack is displayed after completing one task.