Java Program to Implement ArrayDeque API

«
»
This Java program Implements ArrayDeque API.Resizable-array implementation of the Deque interface. Array deques have no capacity restrictions; 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. This class is likely to be faster than Stack when used as a stack, and faster than LinkedList when used as a queue.

Here is the source code of the Java Program to Implement ArrayDeque API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.ArrayDeque;
  2. import java.util.Collection;
  3. import java.util.Iterator;
  4.  
  5. public class ArrayDequeImpl<E>
  6. {
  7.     private ArrayDeque<E> arrayDeque;
  8.  
  9.     /**
  10.      * Constructs an empty array deque with an initial capacity sufficient to
  11.      * hold 16 elements.
  12.      **/
  13.     public ArrayDequeImpl()
  14.     {
  15.         arrayDeque = new ArrayDeque<E>();
  16.     }
  17.  
  18.     /**
  19.      * Constructs a deque containing the elements of the specified collection,
  20.      * in the order they are returned by the collection's iterator.
  21.      **/
  22.     public ArrayDequeImpl(Collection<? extends E> c)
  23.     {
  24.         arrayDeque = new ArrayDeque<E>(c);
  25.     }
  26.  
  27.     /**
  28.      * Constructs an empty array deque with an initial capacity sufficient to
  29.      * hold the specified number of elements.
  30.      **/
  31.     public ArrayDequeImpl(int numElements)
  32.     {
  33.         arrayDeque = new ArrayDeque<E>(numElements);
  34.     }
  35.  
  36.     /** Inserts the specified element at the tail of this queue. **/
  37.     public boolean add(E e)
  38.     {
  39.         return arrayDeque.add(e);
  40.     }
  41.  
  42.     /** Atomically removes all of the elements from this queue. **/
  43.     public void clear()
  44.     {
  45.         arrayDeque.clear();
  46.     }
  47.  
  48.     /** Returns true if this queue contains the specified element. **/
  49.     public boolean contains(Object o)
  50.     {
  51.         return arrayDeque.contains(o);
  52.     }
  53.  
  54.     /** Returns an iterator over the elements in this queue in proper sequence. **/
  55.     public Iterator<E> iterator()
  56.     {
  57.         return arrayDeque.iterator();
  58.     }
  59.  
  60.     /**
  61.      * Inserts the specified element at the tail of this queue if it is possible
  62.      * to do so immediately without exceeding the queue's capacity, returning
  63.      * true upon success and false if this queue is full.
  64.      **/
  65.     public boolean offer(E e)
  66.     {
  67.         return arrayDeque.offer(e);
  68.     }
  69.  
  70.     /**
  71.      * Retrieves, but does not remove, the head of this queue, or returns null
  72.      * if this queue is empty.
  73.      **/
  74.     public E peek()
  75.     {
  76.         return arrayDeque.peek();
  77.     }
  78.  
  79.     /**
  80.      * Retrieves and removes the head of this queue, or returns null if this
  81.      * queue is empty.
  82.      **/
  83.     public E poll()
  84.     {
  85.         return arrayDeque.poll();
  86.     }
  87.  
  88.     /**
  89.      * Removes a single instance of the specified element from this queue, if it
  90.      * is present.
  91.      **/
  92.     public boolean remove(Object o)
  93.     {
  94.         return arrayDeque.remove(o);
  95.     }
  96.  
  97.     /** Returns the number of elements in this queue. **/
  98.     public int size()
  99.     {
  100.         return arrayDeque.size();
  101.     }
  102.  
  103.     /**
  104.      * Returns an array containing all of the elements in this queue, in proper
  105.      * sequence.
  106.      **/
  107.     public Object[] toArray()
  108.     {
  109.         return arrayDeque.toArray();
  110.     }
  111.  
  112.     /**
  113.      * Returns an array containing all of the elements in this queue, in proper
  114.      * sequence; the runtime type of the returned array is that of the specified
  115.      * array.
  116.      **/
  117.     public <T> T[] toArray(T[] a)
  118.     {
  119.         return arrayDeque.toArray(a);
  120.     }
  121.  
  122.     /** Returns a string representation of this collection. **/
  123.     public String toString()
  124.     {
  125.         return arrayDeque.toString();
  126.     }
  127.  
  128.     /** Inserts the specified element at the front of this deque. **/
  129.     public void addFirst(E e)
  130.     {
  131.         arrayDeque.addFirst(e);
  132.     }
  133.  
  134.     /** Inserts the specified element at the end of this deque. **/
  135.     public void addLast(E e)
  136.     {
  137.         arrayDeque.addLast(e);
  138.     }
  139.  
  140.     /** Retrieves, but does not remove, the first element of this deque. **/
  141.     public void getFirst()
  142.     {
  143.         arrayDeque.getFirst();
  144.     }
  145.  
  146.     /** Retrieves, but does not remove, the last element of this deque. **/
  147.     public void getLast()
  148.     {
  149.         arrayDeque.getLast();
  150.     }
  151.  
  152.     /** Inserts the specified element at the front of this deque. **/
  153.     public boolean offerFirst(E e)
  154.     {
  155.         return arrayDeque.offerFirst(e);
  156.     }
  157.  
  158.     /** Inserts the specified element at the end of this deque. **/
  159.     public boolean offerLast(E e)
  160.     {
  161.         return arrayDeque.offerLast(e);
  162.     }
  163.  
  164.     /**
  165.      * Retrieves, but does not remove, the first element of this deque, or
  166.      * returns null if this deque is empty.
  167.      **/
  168.     public E peekFirst()
  169.     {
  170.         return arrayDeque.peekFirst();
  171.     }
  172.  
  173.     /**
  174.      * Retrieves, but does not remove, the last element of this deque, or
  175.      * returns null if this deque is empty.
  176.      **/
  177.     public E peekLast()
  178.     {
  179.         return arrayDeque.peekLast();
  180.     }
  181.  
  182.     public static void main(String... arg)
  183.     {
  184.         ArrayDequeImpl<Integer> arrayDeque = new ArrayDequeImpl<Integer>();
  185.         arrayDeque.add(300);
  186.         arrayDeque.add(200);
  187.         arrayDeque.add(600);
  188.         arrayDeque.add(-400);
  189.         arrayDeque.add(240);
  190.  
  191.         System.out.println("the elements of the arrayDeque is ");
  192.         Iterator<Integer> itr = arrayDeque.iterator();
  193.         while (itr.hasNext())
  194.         {
  195.             System.out.print(itr.next() + "\t");
  196.         }
  197.         System.out.println();
  198.         arrayDeque.offer(600);
  199.         arrayDeque.offer(700);
  200.         arrayDeque.offerFirst(3);
  201.         arrayDeque.offerLast(10);
  202.  
  203.         System.out.println("the peak element of the arrayDeque is(by peeking) " + arrayDeque.peek());
  204.         System.out.println("the peak element of the arrayDeque is(by polling) " + arrayDeque.poll());
  205.         System.out.println("the last element of arrayDeque is (peeking)" + arrayDeque.peekLast());
  206.         System.out.println("element 300 removed " + arrayDeque.remove(300));
  207.         System.out.println("the arrayDeque contains 400 :" + arrayDeque.contains(400));
  208.         System.out.println("the arrayDeque contains 600 :" + arrayDeque.contains(600));
  209.         System.out.println("the size of the arrayDeque is " + arrayDeque.size());
  210.         System.out.println(arrayDeque);
  211.     }
  212. }

$ javac ArrayDequeImpl.java
$ java ArrayDequeImpl
the elements of the arrayDeque is 
300	200	600	-400	240	
the peak element of the arrayDeque is(by peeking) 3
the peak element of the arrayDeque is(by polling) 3
the last element of arrayDeque is (peeking)10
element 300 removed true
the arrayDeque contains 400 :false
the arrayDeque contains 600 :true
the size of the arrayDeque is 7
[200, 600, -400, 240, 600, 700, 10]

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.