ArrayDeque in Java

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 the sides of the queue. Few important features of ArrayDeque are as follows:

  • Array deques have no capacity restrictions and they grow as necessary to support usage.
  • They are not thread-safe which means that in the absence of external synchronization, ArrayDeque does not support concurrent access by multiple threads.
  • Null elements are prohibited in the ArrayDeque.
  • ArrayDeque class is likely to be faster than Stack when used as a stack.
  • ArrayDeque class is likely to be faster than LinkedList when used as a queue.

Declaration:

public class ArrayDeque Element
   extends AbstractCollection
   implements DequeElement, Cloneable, Serializable

Here, Element refers to the element which can refer to any class, such as Integer or String class.

Constructors in ArrayDeque:

  1. ArrayDeque(): Used to create an empty ArrayDeque and by default holds an initial capacity to hold 16 elements.
  2. ArrayDeque(Collection c): Used to create an ArrayDeque containing all the elements same as that of the specified collection.
  3. ArrayDeque(int numofElements): Used to create an empty ArrayDeque and holds the capacity to contain a specified number of elements.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate few functions of
// ArrayDeque in Java
  
import java.util.*;
public class ArrayDequeDemo
{
    public static void main(String[] args)
    {
        // Intializing an deque
        Deque<Integer> de_que = new ArrayDeque<Integer>(10);
   
        // add() method to insert
        de_que.add(10);
        de_que.add(20);
        de_que.add(30);
        de_que.add(40);
        de_que.add(50);
        for (Integer element : de_que)
        {
            System.out.println("Element : " + element);
        }
   
        System.out.println("Using clear() ");
  
        // clear() method
        de_que.clear();
   
        // addFirst() method to insert at start
        de_que.addFirst(564);
        de_que.addFirst(291);
   
        // addLast() method to insert at end
        de_que.addLast(24);
        de_que.addLast(14);
   
        System.out.println("Above elements are removed now");
   
        // Iterator() :
        System.out.println("Elements of deque using Iterator :");
        for(Iterator itr = de_que.iterator(); itr.hasNext();)
        {
            System.out.println(itr.next());
        }
   
        // descendingIterator() : to reverse the deque order
        System.out.println("Elements of deque in reverse order :");
        for(Iterator dItr = de_que.descendingIterator(); 
                                               dItr.hasNext();)
        {
            System.out.println(dItr.next());
        }
   
        // element() method : to get Head element
        System.out.println("\nHead Element using element(): " +
                                             de_que.element());
   
        // getFirst() method : to get Head element
        System.out.println("Head Element using getFirst(): "
                                               de_que.getFirst());
   
        // getLast() method : to get last element
        System.out.println("Last Element using getLast(): "
                                                de_que.getLast());
   
        // toArray() method :
        Object[] arr = de_que.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]);
              
        // peek() method : to get head
        System.out.println("\nHead element : " + de_que.peek());
          
        // poll() method : to get head
        System.out.println("Head element poll : " + de_que.poll());
          
        // push() method :
        de_que.push(265);
        de_que.push(984);
        de_que.push(2365);
          
        // remove() method : to get head
        System.out.println("Head element remove : " + de_que.remove());
          
        System.out.println("The final array is: "+de_que);
    }
}

chevron_right


Output:

Element : 10
Element : 20
Element : 30
Element : 40
Element : 50
Using clear() 
Above elements are removed now
Elements of deque using Iterator :
291
564
24
14
Elements of deque in reverse order :
14
24
564
291

Head Element using element(): 291
Head Element using getFirst(): 291
Last Element using getLast(): 14

Array Size : 4
Array elements :  291 564 24 14
Head element : 291
Head element poll : 291
Head element remove : 2365
The final array is: [984, 265, 564, 24, 14]

Methods in ArrayDeque:

  1. add(Element e) : The method inserts particular element at the end of the deque.
  2. addFirst(Element e) : The method inserts particular element at the start of the deque.
  3. addLast(Element e) : The method inserts particular element at the end of the deque. It is similiar to add() method
  4. clear() : The method removes all deque elements.
  5. size() : The method returns the no. of elements in deque.
  6. clone() : The method copies the deque.
  7. contains(Obj) : The method checks whether a deque contains the element or not
  8. Iterator() : The method returns an iterator over the deque.
  9. descendingIterator() : The method returns a reverse order iterator over the deque
  10. element() : The method returns element at the head of the deque
  11. getFirst(): The method returns first element of the deque
  12. getLast(): The method returns last element of the deque
  13. isEmpty(): The method checks whether the deque is empty or not.
  14. toArray(): The method returns array having the elements of deque.
  15. offer(Element e) : The method inserts element at the end of deque.
  16. offerFirst(Element e) : The method inserts element at the front of deque.
  17. offerLast(Element e) : The method inserts element at the end of deque.
  18. peek() : The method returns head element without removing it.
  19. peekFirst() : The method returns first element without removing it.
  20. peekLast() : The method returns last element without removing it.
  21. poll() : The method returns head element and also removes it
  22. pollFirst() : The method returns first element and also removes it
  23. pollLast() : The method returns last element and also removes it
  24. pop() : The method pops out an element for stack repesented by deque
  25. push(Element e) : The method pushes an element onto stack repesented by deque
  26. remove() : The method returns head element and also removes it
  27. removeFirst() : The method returns first element and also removes it
  28. removeLast() : The method returns last element and also removes it
  29. removeFirstOccurrence(Obj) : The method removes the element where it first occur in the deque.
  30. removeLastOccurrence(Obj) : The method removes the element where it last occur in the deque.

Related Articles:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Codextor