Java Utility Library
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java.util.ArrayDeque Class



Java ArrayDeque Class

Java.util package provides an ArrayDeque class which provides resizable-array implementation of the Deque interface. An Array deque has no capacity restrictions and grows as necessary to support usage. It is not thread-safe; in the absence of external synchronization. It do not support concurrent access by multiple threads. Null elements are prohibited in ArrayDeque. It is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

ArrayDeque class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.

Class declaration

The declaration of java.util.ArrayDeque class is:

public class ArrayDeque<E>
  extends AbstractCollection<E>
    implements Deque<E>, Cloneable, Serializable  

Here, E is the type of element maintained by the container.

Class Constructors

S.NConstructors & Description
1. ArrayDeque()
Creates an empty array deque with initial capacity to hold 16 elements.
2. ArrayDeque(Collection<? extends E> c)
Creates a deque containing the elements of the specified collection, in the order they are returned by the collection's iterator.
3. ArrayDeque(int numElements)
Creates an empty array deque with an initial capacity sufficient to hold the specified number of elements.

java.util.ArrayDeque Methods

The java.util.ArrayDeque class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. boolean add(E element)
Adds a new element at the end of the deque.
2. void addFirst(E element)
Adds a new element at the front of the deque.
3. void addLast(E element)
Adds a new element at the end of the deque.
4. void clear()
Clears all elements of the deque.
5. ArrayDeque<E> clone()
Returns a copy of the deque instance.
6. boolean contains(Object obj)
Returns true if the deque contains the specified element.
7. Iterator<E> descendingIterator()
Returns an iterator over the elements in the deque in reverse sequential order.
8. E element()
Retrieves, but does not remove, the head (first element) of the queue represented by the deque.
9. E getFirst()
Retrieves, but does not remove, the first element of the deque.
10. E getLast()
Retrieves, but does not remove, the last element of the deque.
11. boolean isEmpty()
Returns true if the deque is empty, else returns false.
12. Iterator<E> iterator()
Returns an iterator over the elements in the deque.
13. boolean offer(E element)
Adds the specified element to the end of the deque.
14. boolean offerFirst(E element)
Inserts the specified element at the front of the deque.
15. boolean offerLast(E element)
Inserts the specified element to the end of the deque.
16. E peek()
Retrieves, but does not remove, the head (first element) of the deque.
17. E peekFirst()
Retrieves, but does not remove, the head (first element) of the deque, or returns null if the deque is empty.
18. E peekLast()
Retrieves, but does not remove, the last element of the deque, or returns null if the deque is empty.
19. E poll()
Retrieves and removes the head (first element) of the deque.
20. E pollFirst()
Retrieves and removes the head (first element) of the deque, or returns null if the deque is empty.
21. E pollLast()
Retrieves and removes the last element of the deque, or returns null if the deque is empty.
22. E pop()
Pops an element from the stack represented by the deque.
23. void push(E element)
Pushes an element onto the stack represented by the deque.
24. E remove()
Retrieves and removes the head of the queue represented by the deque.
25. boolean remove(Object obj)
Removes a single instance of the specified element from the deque.
26. E removeFirst()
Removes and returns the first element from the deque.
27. boolean removeFirstOccurrence(Object obj)
Removes the first occurrence of the specified element in this deque (when traversing the deque from head to tail).
28. E removeLast()
Removes and returns the last element from the deque.
29. boolean removeLastOccurrence(Object obj)
Removes the last occurrence of the specified element in this deque (when traversing the deque from head to tail).
30. int size()
Returns the number of elements in the deque.
31. Spliterator<E> spliterator()
Creates a late-binding and fail-fast spliterator over the elements in the deque.
32. Object[] toArray()
Returns an array containing all of the elements in this deque in proper sequence (from first to last element).
33. <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this deque in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractCollection<E>

5