Java.util.ArrayDeque Class

Advertisements


Introduction

The java.util.ArrayDeque class provides resizable-array and implements the Deque interface. Following are the important points about Array Deques:

  • Array deques have no capacity restrictions so they grow as necessary to support usage.

  • They are not thread-safe; in the absence of external synchronization.

  • They do not support concurrent access by multiple threads.

  • Null elements are prohibited in the array deques.

  • They are faster than Stack and LinkedList.

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

Class declaration

Following is the declaration for java.util.ArrayDeque class:

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

Here <E> represents an Element, which could be any class. For example, if you're building an array list of Integers then you'd initialize it as

ArrayList<Integer> list = new ArrayList<Integer>();  

Class constructors

S.N.Constructor & Description
1ArrayDeque()
This constructor is used to create an empty array deque with an initial capacity sufficient to hold 16 elements.
2ArrayDeque(Collection<? extends E> c)
This constructor is used to create a deque containing the elements of the specified collection.
3ArrayDeque(int numElements)
This constructor is used to create an empty array deque with an initial capacity sufficient to hold the specified number of elements.

Class methods

S.N.Method & Description
1boolean add(E e)
This method inserts the specified element at the end of this deque.
2void addFirst(E e)
This method inserts the specified element at the front of this deque.
3void addLast(E e)
This method inserts the specified element at the end of this deque.
4void clear()
This method removes all of the elements from this deque.
5ArrayDeque<E> clone()
This method returns a copy of this deque.
6boolean contains(Object o)
This method returns true if this deque contains the specified element.
7Iterator<E> descendingIterator()
This method returns an iterator over the elements in this deque in reverse sequential order.
8E element()
This method retrieves, but does not remove, the head of the queue represented by this deque.
9E getFirst()
This method retrieves, but does not remove, the first element of this deque.
10E getLast()
This method retrieves, but does not remove, the last element of this deque.
11boolean isEmpty()
This method returns true if this deque contains no elements.
12Iterator<E> iterator()
This method returns an iterator over the elements in this deque.
13boolean offer(E e)
This method inserts the specified element at the end of this deque.
14boolean offerFirst(E e)
This method inserts the specified element at the front of this deque.
15boolean offerLast(E e)
This method inserts the specified element at the end of this deque.
16E peek()
This method retrieves, but does not remove, the head of the queue represented by this deque, or returns null if this deque is empty.
17E peekFirst()
This method retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
18E peekLast()
This method retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
19E poll()
This method retrieves and removes the head of the queue represented by this deque, or returns null if this deque is empty.
20E pollFirst()
This method retrieves and removes the first element of this deque, or returns null if this deque is empty.
21E pollLast()
This method retrieves and removes the last element of this deque, or returns null if this deque is empty.
22E pop()
This method pops an element from the stack represented by this deque.
23void push(E e)
This method pushes an element onto the stack represented by this deque.
24E remove()
This method retrieves and removes the head of the queue represented by this deque.
25boolean remove(Object o)
This method removes a single instance of the specified element from this deque.
26E removeFirst()
This method retrieves and removes the first element of this deque.
27boolean removeFirstOccurrence(Object o)
This method removes the first occurrence of the specified element in this deque.
28E removeLast()
This method retrieves and removes the last element of this deque.
29boolean removeLastOccurrence(Object o)
This method removes the last occurrence of the specified element in this deque.
30int size()
This method returns the number of elements in this deque.
31object[] toArray()
This method returns an array containing all of the elements in this deque in proper sequence.



Advertisements
Advertisements