Week 69 — What is the purpose of `ArrayDeque` and how can it be used?

Question of the Week #69
What is the purpose of ArrayDeque and how can it be used?
5 Replies
Eric McIntyre
Eric McIntyre8mo ago
Deque is an interface for data structures allowing access on both ends. It is possible to insert, retrieve and remove elements both at the beginning and end of a Deque. Similar to ArrayList being an implementation of List, ArrayDeque is an implementation of the Deque interface.
Deque<String> deq = new ArrayDeque<>();

//add at end
deq.add("Hello");
deq.add("World");

//add at beginning
deq.addFirst(":)");
deq.addFirst("start");

//remove at end
String last = deq.removeLast();
System.out.println("last element is: " + last);//last element is World

//do that again
last = deq.removeLast();
System.out.println("last element is: " + last);//last element is Hello

//remove first element
String first = deq.removeFirst();
System.out.println("first element is: " + first);//last element is start

//get first element without removing
first = deq.getFirst();
System.out.println("first element is: " + first);//last element is :)

System.out.println(deq);//[:)]
Deque<String> deq = new ArrayDeque<>();

//add at end
deq.add("Hello");
deq.add("World");

//add at beginning
deq.addFirst(":)");
deq.addFirst("start");

//remove at end
String last = deq.removeLast();
System.out.println("last element is: " + last);//last element is World

//do that again
last = deq.removeLast();
System.out.println("last element is: " + last);//last element is Hello

//remove first element
String first = deq.removeFirst();
System.out.println("first element is: " + first);//last element is start

//get first element without removing
first = deq.getFirst();
System.out.println("first element is: " + first);//last element is :)

System.out.println(deq);//[:)]
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre8mo ago
The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue. Efficient: The ArrayDeque class provides constant-time performance for inserting and removing elements from both ends of the queue, making it a good choice for scenarios where you need to perform many add and remove operations. Resizable: The ArrayDeque class uses a resizable array to store its elements, which means that it can grow and shrink dynamically to accommodate the number of elements in the queue. Lightweight: The ArrayDeque class is a lightweight data structure that does not require additional overhead, such as linked list nodes, making it a good choice for scenarios where memory is limited. Thread-safe: The ArrayDeque class is not thread-safe, but you can use the Collections.synchronizedDeque method to create a thread-safe version of the ArrayDeque class.
Submission from rahulsaini_1957
Eric McIntyre
Eric McIntyre8mo ago
An arraydeque allows an array to have a mutable size with a remove and add method with the normal benefits of an array. Now the use of linked lists is less needed because one item can be added to the back (and front) and the use of an array list is no longer needed since, if it reaches capacity, it needs double (or typically 1.5x) the space to run the add operation
Submission from _draws
Eric McIntyre
Eric McIntyre8mo ago
ArrayDeque - Array Double-Ended Queue The purpose of array deque is to have access from both ends of the array. It can be used to implement a stack or queue.
import java.util.ArrayDeque;
public class Main {
public static void main(String[] args) {

System.out.println("************************************");
System.out.println("- Stack Implementation using ArrayDeque -");

// Stack Implementation Code
ArrayDeque<Integer> stack = new ArrayDeque<>();

// push 3 elements in stack
for(int i = 0; i<3; i++)
stack.addLast(i);

// print stack
System.out.println("\t Stack: " + stack);

// pop an element from stack
int popElement = stack.removeLast();
System.out.println("\t Element popped: " + popElement);
// print Stack
System.out.println("\t Stack: " + stack);

System.out.println("************************************");
System.out.println("- Queue Implementation using ArrayDeque -");

// Queue Implementation Code
ArrayDeque<Integer> queue = new ArrayDeque<>();

// push 3 elements in queue
for(int i = 0; i<3; i++)
queue.addLast(i);

// print queue
System.out.println("\t Queue: " + queue);

// poll an element from queue
int pollElement = queue.removeFirst();
System.out.println("\t Element polled: " + pollElement);
// print queue
System.out.println("\t queue: " + queue);

System.out.println("************************************");
}
}
import java.util.ArrayDeque;
public class Main {
public static void main(String[] args) {

System.out.println("************************************");
System.out.println("- Stack Implementation using ArrayDeque -");

// Stack Implementation Code
ArrayDeque<Integer> stack = new ArrayDeque<>();

// push 3 elements in stack
for(int i = 0; i<3; i++)
stack.addLast(i);

// print stack
System.out.println("\t Stack: " + stack);

// pop an element from stack
int popElement = stack.removeLast();
System.out.println("\t Element popped: " + popElement);
// print Stack
System.out.println("\t Stack: " + stack);

System.out.println("************************************");
System.out.println("- Queue Implementation using ArrayDeque -");

// Queue Implementation Code
ArrayDeque<Integer> queue = new ArrayDeque<>();

// push 3 elements in queue
for(int i = 0; i<3; i++)
queue.addLast(i);

// print queue
System.out.println("\t Queue: " + queue);

// poll an element from queue
int pollElement = queue.removeFirst();
System.out.println("\t Element polled: " + pollElement);
// print queue
System.out.println("\t queue: " + queue);

System.out.println("************************************");
}
}
Output: ************************************ - Stack Implementation using ArrayDeque - Stack: [0, 1, 2] Element popped: 2 Stack: [0, 1] ************************************ - Queue Implementation using ArrayDeque - Queue: [0, 1, 2] Element popped: 0 queue: [1, 2] ************************************
Eric McIntyre
Eric McIntyre8mo ago
⭐ Submission from sk001
Want results from more Discord servers?
Add your server