Arraydeque in java: ArrayDeque in Java is a class that provides the facility of using a deque and resizable array. it extends the AbstractCollection class and implements the Deque interface. ArrayDeque is also known as Array Double Ended Queue that allows the user to add or remove an element from both sides of the Queue. ArrayDeque creates a dynamic array that has no capacity restriction.
- Interfaces Implemented by ArrayDeque
- ArrayDeque Class Declaration
- Hierarchy of ArrayDeque in Java
- Points to Remember about Java ArrayDeque
- Constructors in Java ArrayDeque
- Operations on ArrayDeque Class
- ArrayDeque Methods in Java
- Features of ArrayDeque
- ArrayDeque in Java Example
Interfaces Implemented by ArrayDeque
Array deque java: ArrayDeque implements the following two interfaces. They are in the below fashion
- Queue Interface
- Deque Interface
Queue Interface: It is an interface that follows the First In First Out Data Structure in which the elements are added from the back.
Deque Interface: It is a Doubly Ended Queue in which you can insert the elements from both sides. It is an interface that implements the Queue.
ArrayDeque is resizable from both sides and all the interfaces in the ArrayDeque are Cloneable, Serializable, Iterable<E>, Collection<E>, Deque<E>, Queue<E>.
ArrayDeque Class Declaration
public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable
Here E is the element that can refer to any class be it String or Integer Class.
Read Similar Articles:
Hierarchy of ArrayDeque in Java
Array deque: ArrayDeque class implements Double Ended Queue to support the addition of elements from both sides. It implements a queue interface to support FIFO Data Structure. The Hierarchy Diagram of Array Deque is shown below.
Points to Remember about Java ArrayDeque
- Implements the Deque interface.
- We can add or remove an element from both sides of the queue.
- ArrayDeque has no capacity restriction.
- Null elements are not allowed.
- Not thread-safe, in the absence of external synchronization.
- ArrayDeque uses both stack and queue When it is used as stack it is faster than the Stack, and when it used to queue it is faster than the LinkedList.
- It adds no methods of its own.
Constructors in Java ArrayDeque
The ArrayDeque defines the following constructors.
ArrayDeque(): The first constructor creates an empty deque. Its initial capacity is 16.
ArrayDeque<E> dq = new ArrayDeque<E>();
ArrayDeque(int numOfElements): The second constructor is used to create an ArrayDeque that has the specified initial capacity.
ArrayDeque<E> dq = new ArrayDeque<E>(Collection col);
ArrayDeque(Collection c): The third constructor is used to create an ArrayDeque that is initialized with the elements of the collection passed in c.
ArrayDeque<E> dq = new ArrayDeque<E>(int numofElements);
Operations on ArrayDeque Class
Java arraydeque: Check in detail on how to perform various commonly performed operations on ArrayDeque. They are as under
Adding Elements: To add an element to the ArrayDeque we can use any of the following methods namely
- add()
- addFirst()
- addLast()
- offer()
- offerFirst()
- offerLast()
// Java program to demonstrate the // addition of elements in ArrayDeque import java.io.*; import java.util.*; public class AddingElementsToArrayDeque { public static void main(String[] args) { // Initializing a deque // since deque is an interface // it is assigned the // ArrayDeque class Deque<String> dq = new ArrayDeque<String>(); // add() method to insert dq.add("The"); dq.addFirst("To"); dq.addLast("Geeks"); // offer() method to insert dq.offer("For"); dq.offerFirst("Welcome"); dq.offerLast("Geeks"); // printing Elements of ArrayDeque to the console System.out.println("ArrayDeque : " + dq); } }
Output:
ArrayDeque : [Welcome, To, The, Geeks, For, Geeks]
Accessing the Elements: Once you add the elements if you wish to access the elements you can go with inbuilt methods such as getFirst(), getLast(), etc.
- getFirst()
- getLast()
- peek()
- peekFirst()
- peekLast()
// Java program to access the // elements of ArrayDeque import java.util.*; import java.io.*; public class AccessingElementsOfArrayDeque { public static void main(String args[]) { // Creating an empty ArrayDeque ArrayDeque<String> de_que = new ArrayDeque<String>(); // Use add() method to add elements into the Deque de_que.add("Welcome"); de_que.add("To"); de_que.add("Geeks"); de_que.add("4"); de_que.add("Geeks"); // Displaying the ArrayDeque System.out.println("ArrayDeque: " + de_que); // Displaying the First element System.out.println("The first element is: " + de_que.getFirst()); // Displaying the Last element System.out.println("The last element is: " + de_que.getLast()); } }
Output:
ArrayDeque: [Welcome, To, Geeks, 4, Geeks] The first element is: Welcome The last element is: Geeks
Removing Elements: To remove an element from the deque there are several methods available. As we can remove from both ends Deque Interface provides us with removeFirst(), removeLast() Methods. In addition to these, the interface also provides poll(), pop(), pollFirst(), pollLast(). You can know about each method described in detail in the further modules.
// Java program to demonstrate the // removal of elements in deque import java.util.*; public class RemoveElementsOfArrayDeque { public static void main(String[] args) { // Initializing a deque Deque<String> dq = new ArrayDeque<String>(); // add() method to insert dq.add("One"); // addFirst inserts at the front dq.addFirst("Two"); // addLast inserts at the back dq.addLast("Three"); // print elements to the console System.out.println("ArrayDeque : " + dq); // remove element as a stack from top/front System.out.println(dq.pop()); // remove element as a queue from front System.out.println(dq.poll()); // remove element from front System.out.println(dq.pollFirst()); // remove element from back System.out.println(dq.pollLast()); } }
Output:
ArrayDeque : [Two, One, Three] Two One Three null
Iterating through the Deque: As we can iterate a Deque from both directions the iterator method of the deque interface has two ways to iterate. One is from the start and the other is from the back. They are namely
- iterator()
- descendingIterator()
// Java program to demonstrate the // iteration of elements in deque import java.util.*; public class IterateArrayDeque { public static void main(String[] args) { // Initializing an deque Deque<String> dq = new ArrayDeque<String>(); // add() method to insert // at the back dq.add("For"); // add element at the front dq.addFirst("Geeks"); // add element at the back dq.addLast("Geeks"); dq.add("is so good"); // Iterate using Iterator interface // from the front of the queue for (Iterator itr = dq.iterator(); itr.hasNext();) { System.out.print(itr.next() + " "); } System.out.println(); // Iterate in reverse // sequence in a queue for (Iterator itr = dq.descendingIterator(); itr.hasNext();) { System.out.print(itr.next() + " "); } } }
Output:
Geeks For Geeks is so good is so good Geeks For Geeks
ArrayDeque Methods in Java
Method | Description |
---|---|
add(Element e) | This method inserts a specified element at the end of the deque. |
addAll(Collection<? extends E> c) | Adds all the elements of a specified collection at the end of the deque similar to calling addLast(E) on each one, in the order, the collection’s iterator returns them. |
addFirst(Element e) | Inserts a particular element at the starting of the deque. |
addLast(Element e) | It inserts a particular element at the ending of the deque and is similar to the add() method |
clear() | This method removes all deque elements. |
clone() | It Copies the Deque. |
contains(Obj) | Checks whether a deque contains an element or not. |
element() | Returns elements at the head of the deque |
forEach(Consumer<? super E> action) | Does the given action for each and every element of the iteration until all the elements are processed or the action throws an exception |
getFirst() | Returns the first element of the deque |
getLast() | Returns the last element of the deque |
isEmpty() | This method checks whether the deque is empty or not. |
iterator() | Returns an iterator over the elements in this deque. |
offer(Element e) | Inserts elements at the ending of the deque. |
offerFirst(Element e) | Inserts element at the front of the deque. |
offerLast(Element e) | This method inserts elements at the end of the deque. |
peek() | It returns the head element without removing it. |
poll() | Returns the head element and also removes it. |
pop() | Pops out an element for stack represented by deque. |
push(Element e) | This method pushes an element onto the stack represented by deque |
remove() | This method returns the head element and also removes it. |
remove(Object o) | It Removes a single instance of the specified element from this deque. |
removeAll(Collection<?> c) | Removes all of this collection’s elements that are also present in the specified collection (optional operation). |
removeFirst() | This method returns the first element and even removes it. |
removeFirstOccurrence(Object o) | Removes the first occurrence of the specified element in this deque (while traversing the deque from start to end). |
removeIf(Predicate<? super Element> filter) | This method removes all the elements of this collection that meet the given predicate condition. |
removeLast() | It returns the last element and even removes it. |
removeLastOccurrence(Object o) | Removes the last occurrence of the specified element in this deque (while traversing the deque from start to end). |
retainAll(Collection<?> c) | Retains only the elements in this collection that are present in the specified collection (optional operation). |
size() | This method returns the number of elements in this deque. |
spliterator() | Creates a late-binding and fail-fast Spliterator over the elements in this deque. |
toArray() | This Method Returns an array containing all of the elements in this deque in proper sequence (from first to the last element). |
toArray(T[] a) | Returns an array containing all of the elements in this deque in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array. |
Features of ArrayDeque
Java arraydeque: There are several important features of ArrayDeque that need to be kept in mind. They are as follows
- ArrayDeque Class provides a Resizable Array Implementation of Deque Interface.
- There are no Capacity Restrictions. Thus it can handle added elements to the collection accordingly and grows.
- It is not synchronized which means it is not thread-safe. Multiple threads can access the Same ArrayDeque at a time.
- There is a restriction for Null Elements in the ArrayDeque.
- It Performs Faster Operations Compared to Stack when used as a Stack.
- It even Performs Faster Operations in Comparison to LinkedList when used as a Queue.
- Array Deque Class returns the iterators as fail fast. If the Deque is modified during an iteration the iterator will throw an exception called ConcurrentModification Exception.
ArrayDeque in Java Example
import java.util.*; class ArrayDequeExample{ public static void main(String args[]){ Deque adq = new ArrayDeque(); //adding the element in the array deque adq.add("Ajay"); adq.add("Vijay"); adq.add("Rahul"); adq.add("Amit"); adq.add("Suresh"); System.out.println("ArrayDeque elements are: "+adq); //insert at the first using addFirst() adq.addFirst("Prashant"); //insert at the end using addLast() adq.addLast("Sushant"); //displaying array deque elements System.out.println("After addFirst() and addLast(): "+adq); //size of the array deque System.out.println("ArrayDeque size: " +adq.size()); //get the first element using getFirst() System.out.println("First element is: " +adq.getFirst()); //get the last element using getLast() System.out.println("Last element is: " +adq.getLast()); //remove all the elements adq.clear(); System.out.println("After removing all elements: " +adq); } }
Output: