ArrayDeque is a class that implements Deque.It can perform faster than Stack when used as a stack and faster than LinkedList when used as a queue.

Deque interface defines some methods that access the element at both ends. Which means that by the methods of this interface we can add and remove the elements at both ends.


 



Example:











package visualbuilder;

import java.util.ArrayDeque;

import java.util.Iterator;

public class UseArrayDeque

{

    public static void main(String arg[])

            {

                        ArrayDeque adObj = new ArrayDeque();


                        //Insertion by using various methods

                        adObj.add("Red");

                        adObj.addFirst("Green");

                        adObj.offerFirst("Blue");  

                        adObj.offerLast("Orange"); 

                                              

                        System.out.println("Retrieving First Element :" + adObj.peekFirst());

                        System.out.println("Retrieving Last Element :" + adObj.peekLast());


                        System.out.println("Removing First  Element :" + adObj.pollFirst());

                        System.out.println("Removing Last  Element :" + adObj.pollLast());


                        //Reverse traversal

                        System.out.println("Remaining Elements :");

                        Iterator it = adObj.descendingIterator();

                        while(it.hasNext())

                        {

                                    System.out.println(it.next());

                        }                     

            }

}




 



Output:


















 


 


 


 

                    

Copyright © 2012 VisualBuilder. All rights reserved