In this tutorial, we will learn about the ArrayDeque, its contructors and methods and how to implement it in Java.
Java Collection frameworks provide ArrayDeque class and it implements Deque Interface.ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue.
Constructors | Description |
---|---|
ArrayDeque() | Used to create an empty ArrayDeque and by default holds an initial capacity to hold 16 elements. |
ArrayDeque(Collection extends E> c) | Used to create an ArrayDeque containing all the elements same as that of the specified collection. |
ArrayDeque(int numofElements) | Used to create an empty ArrayDeque and holds the capacity to contain a specified number of elements. |
Apart from the methods inherited from its parent class/interface ,it has following methods :
Methods | Description |
---|---|
add(Element e) | The method inserts particular element at the end of the deque. |
addFirst(Element e) | The method inserts particular element at the start of the deque. |
addLast(Element e) | The method inserts particular element at the end of the deque. It is similiar to add() method. |
clear() | The method removes all deque elements. |
size() | The method returns the no. of elements in deque. |
clone() | The method copies the deque. |
contains(Obj) | The method checks whether a deque contains the element or not. |
Iterator() | The method returns an iterator over the deque. |
descendingIterator() | The method returns a reverse order iterator over the deque. |
element() | The method returns element at the head of the deque. |
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. |
toArray() | The method returns array having the elements of 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 deque. |
peek() | The method returns head element without removing it. |
peekFirst() | The method returns first element without removing it. |
peekLast() | The method returns last element without removing it. |
poll() | The method returns head element and also removes it. |
pollFirst() | The method returns first element and also removes it. |
pollLast() | The method returns last element and also removes it. |
pop() | The method pops out an element for stack repesented by deque. |
push(Element e) | The method pushes an element onto stack repesented by deque. |
remove() | The method returns head element and also removes it. |
removeFirst() | The method returns first element and also removes it. |
removeLast() | The method returns last element and also removes it. |
removeFirstOccurrence(Obj) | The method removes the element where it first occur in the deque. |
removeLastOccurrence(Obj) | The method removes the element where it last occur in the deque. |
// Java code explaining the use of ArrayDeque class methods
import java.util.*;
public class CodeMistic
{
public static void main(String[] args)
{
// Intializing an deque
Deque<Integer> d = new ArrayDeque<Integer>(10);
// add() method to insert
d.add(5);
d.add(8);
d.add(12);
d.add(9);
d.add(1);
for (Integer element : d)
{
System.out.println("Element : " + element);
}
System.out.println("Using clear() ");
//clear() method
d.clear();
// addFirst() method to insert at start
d.addFirst(10);
d.addFirst(20);
// addLast() method to insert at end
d.addLast(24);
d.addLast(14);
System.out.println("Above elements are removed now \n");
//size() of ArrayDeque
System.out.println("Size of deque : " + d.size() + "\n");
for (Integer element : d)
{
System.out.println("Element : " + element);
}
// conatins() method
System.out.println("\nIs 10 present in deque : " + d.contains(10));
// Iterator() :
System.out.println("\nElements of deque using Iterator :");
for(Iterator itr = d.iterator(); itr.hasNext();)
{
System.out.println(itr.next());
}
// descendingIterator() : to reverse the deque order
System.out.println("\nElements of deque in reverse order :");
for(Iterator dItr = d.descendingIterator(); dItr.hasNext();)
{
System.out.println(dItr.next());
}
// element() method : to get Head element
System.out.println("\nHead Element using element(): " + d.element());
// getFirst() method : to get Head element
System.out.println("Head Element using getFirst(): " + d.getFirst());
// getLast() method : to get last element
System.out.println("Last Element using getLast(): " + d.getLast());
// isEmpty() method :
System.out.println("\nChecks whether deque is empty : " + d.isEmpty());
//toArray() method :
Object[] arr = d.toArray();
System.out.println("\nArray Size : " + arr.length);
System.out.print("Array elements : ");
for(int i=0; i<arr.length ; i++)
System.out.print(" " + arr[i]);
}
}
Output:
Element : 5
Element : 8
Element : 12
Element : 9
Element : 1
Using clear()
Above elements are removed now
Size of deque : 4
Element : 20
Element : 10
Element : 24
Element : 14
Is 10 present in deque : true
Elements of deque using Iterator :
20
10
24
14
Elements of deque in reverse order :
14
24
10
20
Head Element using element(): 20
Head Element using getFirst(): 20
Last Element using getLast(): 14
Checks whether deque is empty : false
Array Size : 4
Array elements : 20 10 24 14