![]() |
VOOZH | about |
ArrayDeque is a resizable-array implementation of the Deque interface in Java. it allows elements to be added or removed from both ends efficiently and it not allow null element insertion .
In Java, the declaration of ArrayDeque can be done as:
ArrayDeque<Type> deque = new ArrayDeque<>();
Here, "Type" is the type of the element the deque will hold (e.g. Integer, String).
First: 1, Last: 2
It implements Queue and Deque interfaces and is a part of the Collection framework.
This constructor is used to create an empty ArrayDeque and by default holds an initial capacity to hold 16 elements.
ArrayDeque<Integer> deque = new ArrayDeque<>();
This constructor is used to create an ArrayDeque containing all the elements the same as that of the specified collection.
ArrayDeque<Integer> deque = new ArrayDeque<>(list);
This constructor is used to create an empty ArrayDeque and holds the capacity to contain a specified number of elements.
ArrayDeque<String> deque = new ArrayDeque<>(25); // capacity = 25
We can use methods like add(), addFirst(), addLast(), offer(), offerFirst(), offerLast() to insert element to the ArrayDeque.
ArrayDeque : [Welcome, To, The, Geeks, For, Geeks]
We can use methods like getFirst(), getLast(), peek(), peekFirst(), peekLast() to access elements of the ArrayDeque.
ArrayDeque: [Welcome, To, Geeks, for, Geeks] The first element is: Welcome The last element is: Geeks
We can use various methods like remove(), removeFirst(), removeLast(), poll(), pollFirst(), pollLast(), pop() to remove elements from the ArrayDeque.
Initial Deque: [C++, Java, Python] Removed element using pop(): C++ Removed element using poll(): Java Removed element using pollFirst(): Python Final Deque: []
We can use various methods like iterator(), descendingIterator() to iterate over the elements of ArrayDeque.
Iterating in ForwardOrder: Geeks For Geeks is so good Iterating in ReverseOrder: is so good Geeks For Geeks
Method | Description |
|---|---|
| add(Element e) | The method inserts a particular element at the end of the deque. |
| addAll(Collection<? extends E> c) | Adds all of the elements in the specified collection at the end of this deque, as if by calling addLast(E) on each one, in the order that they are returned by the collection's iterator. |
| addFirst(Element e) | The method inserts particular element at the start of the deque. |
| addLast(Element e) | The method inserts a particular element at the end of the deque. It is similar to the add() method |
| clear() | The method removes all deque elements. |
| clone() | The method copies the deque. |
| contains(Obj) | The method checks whether a deque contains the element or not |
| element() | The method returns element at the head of the deque |
| forEach(Consumer<? super E> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
| getFirst() | The method returns first element of the deque |
| getLast() | The method returns last element of the deque |
| isEmpty() | The method checks whether the deque is empty or not. |
| iterator() | Returns an iterator over the elements in this deque. |
| offer(Element e) | The method inserts element at the end of deque. |
| offerFirst(Element e) | The method inserts element at the front of deque. |
| offerLast(Element e) | The method inserts element at the end of the deque. |
| peek() | The method returns head element without removing it. |
| poll() | The method returns head element and also removes it |
| pop() | The method pops out an element for stack represented by deque |
| push(Element e) | The method pushes an element onto stack represented by deque |
| remove() | The method returns head element and also removes it |
| remove(Object o) | Removes a single instance of the specified element from this deque. |
| removeAll(Collection<?> c) | Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
| removeFirst() | The method returns the first element and also removes it |
| removeFirstOccurrence(Object o) | Removes the first occurrence of the specified element in this deque (when traversing the deque from head to tail). |
| removeIf(Predicate<? super Element> filter) | Removes all of the elements of this collection that satisfy the given predicate. |
| removeLast() | The method returns the last element and also removes it |
| removeLastOccurrence(Object o) | Removes the last occurrence of the specified element in this deque (when traversing the deque from head to tail). |
| size() | Returns the number of elements in this deque. |
| spliterator() | Creates a late-binding and fail-fast Spliterator over the elements in this deque. |
| toArray() | Returns an array containing all of the elements in this deque in proper sequence (from first to the last element). |
Method | Action Performed |
|---|---|
| descendingIterator() | Returns an iterator over the elements in this deque in reverse sequential order. |
| peekFirst() | Retrieves, but does not remove, the first element of this deque or returns null if this deque is empty. |
| pollFirst() | Retrieves and removes the first element of this deque or returns null if this deque is empty. |
| pollLast() | Retrieves and removes the last element of this deque or returns null if this deque is empty. |