Menu
Topics Index
...
`


Collections Framework > Collection Classes >
Siva Nookala - 15 Apr 2016
ArrayDeque class, which extends AbstractCollection and implements the Deque interface. It adds no methods of its own. ArrayDeque creates a dynamic array and has no capacity restrictions.

ArrayDeque is faster than Stack and LinkedList and doesn't permit null elements. ArrayDeque is a generic class that has the following declaration:

class ArrayDeque< E >
Here, E specifies the type of objects stored in the collection. ArrayDeque defines the following constructors:
ConstructorExplanation
ArrayDeque()It builds an empty deque
ArrayDeque(int size)It builds an dequethat has specified initial capacity.
ArrayDeque(Collection< ? extends E > c)It creates a dequethat is initialized with the elements of the collection c.
ArrayDeque defines the following Methods:
Method NameExplanation
element()Returns the element at the head of the queue. The element is not removed. It throws NoSuchElementException if the queue is empty.
offer(E obj)Attempts to add obj to the queue. Returns true if obj was added and false otherwise.
peek()Returns the element at the head of the queue. It returns null if the queue is empty. The element is not removed.
poll()Returns the element at the head of the queue, removing the element in the process. It returns null if the queue is empty
remove()Removes the element at the head of the queue, removing the element in the process. t throws NoSuchElementException if the queue is empty.
addFirst(E obj)Adds obj to the head of the deque. Throws an IllegalStateException if a capacity-restricted deque is out of space.
addLast(E obj)Adds obj to the tail of the deque. Throws an IllegalStateException if a capacity-restricted deque is out of space.
getFirst()Returns the first element in the deque. The object is not removed from the deque. It throws NoSuchElementException if the deque is empty.
getLast()Returns the last element in the deque. The object is not removed from the deque. It throws NoSuchElementException if the deque is empty.
offerFirst(E obj)Attempts to add obj to the head of the deque. Returns true if obj was added and false otherwise. Therefore, this method returns false when an attempt is made to add obj to a full, capacity-restricted deque
offerLast(E obj)Attempts to add obj to the tail of the deque. Returns true if obj was added and false otherwise.
peekFirst()Returns the element at the head of the deque. It returns null if the deque is empty. The object is not removed.
peekLast()Returns the element at the tail of the deque. It returns null if the deque is empty. The object is not removed.
pollFirst()Returns the element at the head of the deque, removing the element in the process, It returns null if the deque is empty.
pollLast()Returns the element at the tail of the deque, removing the element in the process, It returns null if the deque is empty.
pop()Returns the element at the head of the deque, removing it in the process. It throws NoSuchElementException if the deque is empty.
push(E obj) Adds obj to the head of the deque. Throws an IllegalStateException if a capacity restricted deque is out of space.
removeFirst()Returns the element at the head of the deque, removing the element in the process. It throws NoSuchElementExcepton if the deque is empty.
removeLast()Returns the element at the tail of the deque, removing the element in the process. It throws NoSuchElementException if the deque is empty.
The following program demonstrates ArrayDeque:
ArrayDeque Class
import java.util.*;        

class ArrayDequeDemo
{
    public static void main(String arg[])
    {
        ArrayDeque<Integer> adq = new ArrayDeque<Integer>();
        //Use an ArrayDeque like a queue
        adq.add(1);
        adq.add(2);
        adq.add(3);
        adq.add(4);
        //adq.add(null); LINE A
        System.out.print("Elements of queue: ");
        while(adq.peek()!=null)
        System.out.print(adq.pop()+"\t");
        System.out.println();
        System.out.print(" Elements of stack: ");
        //Use an ArrayDeque like a stack
        adq.push(1);
        adq.push(2);
        adq.push(3);
        adq.push(4);
        while(adq.peek()!=null)
        System.out.print(adq.pop()+"\t");
    
    }
}
OUTPUT

Elements of queue: 1    2    3    4    
Elements of stack: 4    3    2    1

DESCRIPTION

The first part of the program demonstrates ArrayDeque by using it to create a queue.Hence it follows "first in first out" order in displaying the elements of the queue.

The second part of the program demonstrates ArrayDeque by using it to create a stack.Hence it follows "last in first out" order in displaying the elements of the stack.

THINGS TO TRY
  • Uncomment LINE A to see java.lang.NullPointerException since ArrayDequeu doesn't permit null elements.
  • Place the below code in the above program.
    System.out.println(adq.pop());
    System.out.println(adq);
    The above code removes the first element from adq so the output will be
    1
    [2, 3, 4]

Dependent Topics : Java ArrayList  java.util.Arrays - Class Arrays In Collection Framework 

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.