An ArrayDeque is a special kind of a growable array that allows us to add or remove an element from both sides. It is an implementation of Deque Interface which allows insertion of elements at both the ends. It does not have any restrictions on capacity. It expands automatically as we add more elements. The ArrayDeque class extends AbstractCollection class and implements Deque interface.
Properties Of ArrayDeque Class :
- ArrayDeque is a resizable-array implementation of Deque interface like ArrayList class which is a resizable-array implementation of List interface. But, ArrayDeque is not a List.
- ArrayDeque does not have any capacity limit. It will grow automatically as we add elements.
Default initial capacity of ArrayDeque is 16. It will increase at a power of 2 (24, 25, 26 and so on) when size exceeds capacity. - ArrayDeque can be used as a stack (LIFO) as well as a queue (FIFO). ArrayDeque is faster than the Stack class when used as a stack and faster than the LinkedList class when used as a queue.
- Performance of ArrayDeque is sometimes considered as the best among the collection framework. It gives performance of O(1) for insertion, removal and retrieval operations. ArrayDeque class is recommended instead of Stack class (when you want stack data structure) and instead of LinkedList class (when you want queue data structure).
Important API :-
- boolean add(E e)
Inserts the specified element at the end of this deque. - void addFirst(E e)
Inserts the specified element at the front of this deque. - void addLast(E e)
Inserts the specified element at the end of this deque. - void clear()
Removes all of the elements from this deque. - boolean contains(Object o)
Returns true if this deque contains the specified element. - E getFirst()
Retrieves, but does not remove, the first element of this deque. - E getLast()
Retrieves, but does not remove, the last element of this deque. - boolean isEmpty()
Returns true if this deque contains no elements. - Iterator<E> iterator()
Returns an iterator over the elements in this deque. - E peekFirst()
Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty. - E peekLast()
Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty. - E pollFirst()
Retrieves and removes the first element of this deque, or returns null if this deque is empty. - E pollLast()
Retrieves and removes the last element of this deque, or returns null if this deque is empty. - int size()
Returns the number of elements in this deque. - E remove()
Retrieves and removes the head of the queue represented by this deque. - boolean remove(Object o)
Removes a single instance of the specified element from this deque. - E removeFirst()
Retrieves and removes the first element of this deque. - E removeLast()
Retrieves and removes the last element of this deque.
Reference :-