ArrayDeque
java.util.ArrayDeque CLASS
REFERENCE:~
- Official Documentation:-
Java 14java.util.ArrayListhttps://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/util/ArrayList.html - ....
Sarthak Uses ArrayDeque to implement both Stack and Queue as ArrayDeque contains all the methods of Stack and Queue
ArrayDequecan be used to implement bothLIFOandFIFO.
To Check: Collections methods can be used with ArrayDeque or not??!⁉️💣
Relation between Deque & ArrayDeque
As we mentioned before, Deque<E> extends Queue<E> and represents a queue where you can insert and remove elements from both ends. It combines access rules provided by queue (FIFO) and stack (LIFO) together.
The Deque interface provides methods for working with the first and the last element of a queue. Some of the methods throw an exception, while others just return a special value (null). Check out the table:
Since ArrayDeque and LinkedList implement this interface, they both can work as a queue (FIFO), a stack (LIFO), or a deque.
initialisation
ArrayList to ArrayDeque
- using
Collections.addAll()
// Collections.addAll(to_be_Name, from_Name);
Collections.addAll(ArrayDequeName, ArrayListName);
- using
DataStructureName.addAll()
// to_name.addAll(from_name)
ArrayDequeName.addAll(ArrayListName);
Array to ArrayDeque
ArrayDeque implements Deque.
- A deque (usually pronounced “deck”) stands for double ended queue and is a combination
of a stack and a queue, in that it supports O(1) insertions and deletions from both the front
and the back of the deque. In Java, the deque class is called ArrayDeque. The four methods
for adding and removing are
offerFirst(),pollFirst(),offerLast(), andpollLast().
:::
🤯 MUST KNOW!
How to use offerFirst(), pollFirst(), offerLast(), pollLast(),peekLast() & peekFirst() for LIFO and FIFO operations as Stack and Queue
For
StackasLIFOuseofferLast()to input elements andpollLast()to print elements. or useofferFirst()to input elements andpollFirst()to print elements.. IfFor
QueueasFIFOuseofferLast()to input elements andpollFirst()to print elements. or useofferFirst()to input elements andpollLast()to print elements.Using
Iteratormethods withoutpollLast()orpollFirst():~ ForStackasLIFOuseofferFirst()to input elements and then iterate usinghasNext()andnext()methods ofIterator.Using
Iteratormethods withoutpollLast()orpollFirst():~ ForQueueasFIFOuseofferLast()to input elements and then iterate usinghasNext()andnext()methods ofIterator.
I recommend using ↴:
- For
StackasLIFOuseofferLast()to input elements andpollLast()to print elements.- For
QueueasFIFOuseofferLast()to input elements andpollFirst()to print elements.
offerFirst()
// input is [ 45 5 89 65 625 165 ]
ArrayDeque<Integer> ad = new ArrayDeque<>();
while (sc.hasNext()) {
ad.offerFirst(sc.nextInt());
} // ArrayDeque becomes: [165, 625, 65, 89, 5, 45]
while (it.hasNext() && (ad.size() != 0)) { // "!ad.isEmpty()" can be used instead of "ad.size() != 0"
System.out.println(ad.pollLast());
}
/* It prints:
45
5
89
65
625
165 */
offerLast()
// input is [ 45 5 89 65 625 165 ]
ArrayDeque<Integer> ad = new ArrayDeque<>();
while (sc.hasNext()) {
ad.offerLast(sc.nextInt());
} // ArrayDeque becomes: [45, 5, 89, 65, 625, 165]
while (it.hasNext() && !ad.isEmpty()) { // "ad.size() != 0" can be used instead of "!ad.isEmpty()"
System.out.println(ad.pollLast());
}
/* It prints:
165
625
65
89
5 */
pollFirst()
for-Each loop cannot be used with pollLast(). So Iterator must be used.
// input is [ 45 5 89 65 625 165 ]
ArrayDeque<Integer> ad = new ArrayDeque<>();
while (sc.hasNext()) {
ad.offerFirst(sc.nextInt());
} // ArrayDeque becomes: [165, 625, 65, 89, 5, 45]
Iterator<Integer> it = ad.iterator();
// "ad.size() != 0" can be used instead of "!ad.isEmpty()"
while (it.hasNext() && !ad.isEmpty()) { // to avoid printing "null" infinity times as "pollFirst()" shows this problem when "ArrayDeque" is used with "Iterator"
System.out.println(ad.pollFirst());
}
/* It prints:
165
625
65
89
5 */
- I use
(ad.size() != 0)to avoid printing "null" infinity times as "pollFirst()" shows this problem when "ArrayDeque" is used with "Iterator" - This error only occurs when
pollFirst,pollLast&pollare used withIterator
pollLast()
for-Each loop cannot be used with pollLast(). So Iterator must be used.
// input is [ 45 5 89 65 625 165 ]
ArrayDeque<Integer> ad = new ArrayDeque<>();
while (sc.hasNext()) {
ad.offerLast(sc.nextInt());
} // ArrayDeque becomes: [45, 5, 89, 65, 625, 165]
Iterator<Integer> it = ad.iterator();
// "!ad.isEmpty()" can be used instead of "ad.size() != 0"
while (it.hasNext() && (ad.size() != 0)) { // to avoid printing "null" infinity times as "pollLast()" shows this problem when "ArrayDeque" is used with "Iterator"
System.out.println(ad.pollLast());
}
/* It prints:
165
625
65
89
5 */
- I use
(ad.size() != 0)to avoid printing "null" infinity times as "pollFirst()" shows this problem when "ArrayDeque" is used with "Iterator" - This error only occurs when
pollFirst,pollLast&pollare used withIterator
peekFirst()
peekLast()
FIFO using offerLast() with hasNext() & next() ~ Iterator
// input is [ 45 5 89 65 625 165 ]
ArrayDeque<Integer> ad = new ArrayDeque<>();
while (sc.hasNext()) {
ad.offerLast(sc.nextInt());
} // ArrayDeque becomes: [45, 5, 89, 65, 625, 165]
Iterator<Integer> it = ad.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
/* It prints:
45
5
89
65
625
165 */
LIFO using offerFirst() with hasNext() & next() ~ Iterator
// input is [ 45 5 89 65 625 165 ]
ArrayDeque<Integer> ad = new ArrayDeque<>();
while (sc.hasNext()) {
ad.offerFirst(sc.nextInt());
} // ArrayDeque becomes: [165, 625, 65, 89, 5, 45]
Iterator<Integer> it = ad.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
/* It prints:
165
625
65
89
5
45 */
ArrayDeque methods as Stack
pop()
- Acts similar to
pop()inStackasLIFO, the Last element inserted comes out First. - It returns the REVERSE ODER of the input
/* Input is: [45 5 89 65 625 165] */
ArrayDeque<Integer> ad = new ArrayDeque<>();
while (sc.hasNext()) {
ad.push(sc.nextInt());
} // ArrayDeque "ad" becomes: [165, 625, 65, 89, 5, 45]
for (int el : ad) {
System.out.println(ad.pop());
} /* It prints:
165
625
65
89
5
45 */
- ❎ On using
pop()withIterator, ArrayDeque shows an error☠️: 👇 BUT also prints asLIFO- ❎ On using
remove()orpoll()orpollLast()orpollFirst()(poll()orpollLast()orpollFirst()throws Infinite Null; not Recommended) withIterator, ArrayDeque shows an error☠️: 👇 BUT also prints asFIFO
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.ArrayDeque.removeFirst(ArrayDeque.java:362)
at java.base/java.util.ArrayDeque.pop(ArrayDeque.java:593) at Main.main(Main.java:33)
- For
pop()andremove()inArraydeque~Iteratorthrows⏬:
NoSuchElementException
- For
poll()orpollLast()orpollFirst()inArrayDeque~Iteratorthrows⏬:
prints "null" infinite times
/* Input is: [45 5 89 65 625 165] */
while (it.hasNext()) {
if (ad.size() == 0) { // for avoiding the Error/Exception ↴
break; // "NoSuchElementException" occurs when the ArrayDeque becomes Empty
} // By checking the 'size()' for "0", we can avoid the Exception
System.out.println(ad.pop());
} /* It prints:
165
625
65
89
5
45 */
Instead of using pop(), I can simply use Iterator and its methods hasNext() & next();; just the trick is I will have to initialise:
ArrayDequeaspush()forStackArrayDequeasoffer()oradd()forQueue
the
add(E e)method does the same asoffer(E e)but throws IllegalStateException if no space is currently available;
For pop() of Stack
/* Input is: [45 5 89 65 625 165] */
ArrayDeque<Integer> ad = new ArrayDeque<>();
while(sc.hasNext()) {
ad.push(sc.nextInt());
} // ArrayDeque "ad" becomes: [165, 625, 65, 89, 5, 45]
Iterator<Integer> it = ad.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} /* It prints:
165
625
65
89
5
45 */
For remove() of Queue
/* Input is: [45 5 89 65 625 165] */
ArrayDeque<Integer> ad = new ArrayDeque<>();
while(sc.hasNext()) {
ad.offer(sc.nextInt()); //"add()" can also be used but not recommended
} // ArrayDeque "ad" becomes: [45, 5, 89, 65, 625, 165]
Iterator<Integer> it = ad.iterator();
while (it.hasNext()) {
System.out.println(it.next());
} /* It prints:
45
5
89
65
625
165 */