java.util.ArrayDeque class describes an implementation of a resizable array structure which implements the Deque interface.
Array deques has not immutable and can grow as necessary. They are not thread-safe and hence concurrent access by multiple threads is not supported unless explicitly synchronized. Null elements are invalid in this structure. Most operations run in constant time, with speed decreasing linearly as size grows.
Declaration
public class ArrayDeque extends AbstractCollection implements Deque, Cloneable, Serializable
Methods of ArrayDeque Class :
- add(Element e) : java.util.ArrayDeque.add(Element e) inserts particular element at the end of the deque.
Syntax :public boolean add(Element e) Parameter e : elememt to add Return true : if element is inserted, else no
- addFirst(Element e) : java.util.ArrayDeque.addFirst(Element e) inserts particular element at the start of the deque.
Syntax :public boolean addFirst(Element e) Parameter e : elememt to add at the start Return true : if element is inserted, else no
- addLast(Element e) : java.util.ArrayDeque.addLast(Element e) inserts particular element at the end of the deque. It is similar to add() method
Syntax :
public boolean addaddLast(Element e) Parameter e : elememt to add at end Return true : if element is inserted, else no
- clear() : java.util.ArrayDeque.clear() removed all deque elements.
Syntax :public void clear() Parameter ------- Return -------
- size() : java.util.ArrayDeque.size() returns the no. of elements in deque.
Syntax :public int size() Parameter ----- Return no. of elements in deque.
- clone() : java.util.ArrayDeque.clone()copies the deque.
Syntax :public ArrayDeque clone() Parameter -------- Return copy of deque
- contains(Obj) : java.util.ArrayDeque.contains(Obj) checks whether a deque contains the element or not
Syntax :public boolean contains(Object obj) Parameter obj : object to be checked Return true, if element is +nt in the deque; else false
- Iterator() : java.util.ArrayDeque.Iterator() returns an iterator over the deque.
Syntax :public Iterator Iterator() Parameter ---------- Return iterator over the deque.
- descendingIterator() : java.util.ArrayDeque.descendingIterator() returns a reverse order iterator over the deque
Syntax :public Iterator descendingIterator() Parameter ---------- Return returns a reverse order iterator over the deque.
- element() : java.util.ArrayDeque.element() returns element at the head of the deque
Syntax :public E element() Parameter ------ Return head element of the deque
- getFirst: java.util.ArrayDeque.getFirst()returns first element of the deque
Syntax :
public E getFirst() Parameter ------ Return head element of the deque
- getLast: java.util.ArrayDeque.getLast()returns last element of the deque
Syntax :public E getLast() Parameter ------ Return last element of the deque
- isEmpty(): java.util.ArrayDeque.isEmpty() checks whether the deque is empty or not.
Syntax :public boolean isEmpty() Parameter ------ Return true if the deque is empty; else false
- toArray(): java.util.ArrayDeque.toArray() returns array having the elements of deque.
Syntax :public Object[] toArray() Parameter ------ Return returns array having the elements of deque.
Java Program explaining ArrayDeque class methods :
// Java code explaining the use of ArrayDeque class methods // add(), addFirst(), addLast(), clear(), size(), conatins() // descendingIterator(), element(), getFirst(), isEmpty(), // toArray, Iterator(), getLast() import java.util.*; public class NewClass { public static void main(String[] args) { // Intializing an deque Deque<Integer> d = new ArrayDeque<Integer>(10); // add() method to insert d.add(2); d.add(4); d.add(6); d.add(8); d.add(2); 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 +nt 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 : 2 Element : 4 Element : 6 Element : 8 Element : 2 Using clear() Above elements are removed now Size of deque : 4 Element : 20 Element : 10 Element : 24 Element : 14 Is 10 +nt 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
Java.util.ArrayDeque Class in Java | Set 2
This article is contributed by Mohit Gupta_OMG 🙂. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.lang.Class class in Java | Set 1
- Java.lang.Class class in Java | Set 2
- Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
- Using predefined class name as Class or Variable name in Java
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Difference between Abstract Class and Concrete Class in Java
- In Java, Can we call the main() method of a class from another class?
- Java Program to Check if a Given Class is a Local Inner Class
- Java Program to Check if a Given Class is an Anonymous Class
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1

