ArrayDeque is a resizable-array implementation of the Deque interface in Java. it allows elements to be added or removed from both ends efficiently and it not allow null element insertion .
ArrayDeque grows dynamically.
It generally provides faster operations than LinkedList as it is more cache friendly and does not require overhead of next previous reference management.
Operations like addFirst(), addLast(), removeFirst(), removeLast() are done in O(1) amortized time.
ArrayDeque is not Thread-Safe
Declaration of ArrayDeque
In Java, the declaration of ArrayDeque can be done as:
ArrayDeque<Type> deque = new ArrayDeque<>();
Here, "Type" is the type of the element the deque will hold (e.g. Integer, String).
importjava.io.*;importjava.util.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Initializing a deque since deque is an interface. it is assigned the ArrayDeque classDeque<String>d=newArrayDeque<String>();// add() method to insertd.add("The");d.addFirst("To");d.addLast("Geeks");// offer() method to insertd.offer("For");d.offerFirst("Welcome");d.offerLast("Geeks");System.out.println("ArrayDeque : "+d);}}
Output
ArrayDeque : [Welcome, To, The, Geeks, For, Geeks]
importjava.io.*;importjava.util.*;publicclassGeeks{publicstaticvoidmain(Stringargs[]){// Creating an empty ArrayDequeArrayDeque<String>d=newArrayDeque<String>();// Using add() method to add elements into the Deque Custom input elementsd.add("Welcome");d.add("To");d.add("Geeks");d.add("for");d.add("Geeks");// Displaying the ArrayDequeSystem.out.println("ArrayDeque: "+d);// Displaying the First elementSystem.out.println("The first element is: "+d.getFirst());// Displaying the Last elementSystem.out.println("The last element is: "+d.getLast());}}
Output
ArrayDeque: [Welcome, To, Geeks, for, Geeks]
The first element is: Welcome
The last element is: Geeks
importjava.util.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Initializing a dequeDeque<String>d=newArrayDeque<String>();// Adding elementsd.add("Java");d.addFirst("C++");d.addLast("Python");// Printing initial elementsSystem.out.println("Initial Deque: "+d);// Removing elements as a stack from top/frontSystem.out.println("Removed element using pop(): "+d.pop());// Removing an element from the frontSystem.out.println("Removed element using poll(): "+d.poll());// Removing an element from the front using pollFirstSystem.out.println("Removed element using pollFirst(): "+d.pollFirst());// The deque is empty nowSystem.out.println("Final Deque: "+d);}}
Output
Initial Deque: [C++, Java, Python]
Removed element using pop(): C++
Removed element using poll(): Java
Removed element using pollFirst(): Python
Final Deque: []
importjava.util.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Declaring and initializing an dequeDeque<String>d=newArrayDeque<String>();// Adding elements at the back using add() methodd.add("For");// Adding element at the front using addFirst() methodd.addFirst("Geeks");// add element at the last using addLast() methodd.addLast("Geeks");d.add("is so good");// Iterate using Iterator interface from the front of the queueSystem.out.println("Iterating in ForwardOrder:");for(Iteratori=d.iterator();i.hasNext();){System.out.print(i.next()+" ");}System.out.println();// Iterate in reverse sequence in a queueSystem.out.println("Iterating in ReverseOrder:");for(Iteratori=d.descendingIterator();i.hasNext();){System.out.print(i.next()+" ");}}}
Output
Iterating in ForwardOrder:
Geeks For Geeks is so good
Iterating in ReverseOrder:
is so good Geeks For Geeks
Adds all of the elements in the specified collection at the end of this deque, as if by calling addLast(E) on each one, in the order that they are returned by the collection's iterator.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.