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 :
1. 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 : element to add Return true : if element is inserted, else no
2. 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 : element to add at the start Return true : if element is inserted, else no
3. 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 : element to add at end Return true : if element is inserted, else no
4. clear() : java.util.ArrayDeque.clear() removed all deque elements.
Syntax :
public void clear() Parameter ------- Return -------
5. size() : java.util.ArrayDeque.size() returns the no. of elements in deque.
Syntax :
public int size() Parameter ----- Return no. of elements in deque.
6. clone() : java.util.ArrayDeque.clone()copies the deque.
Syntax :
public ArrayDeque clone() Parameter -------- Return copy of deque
7. 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
8. Iterator() : java.util.ArrayDeque.Iterator() returns an iterator over the deque.
Syntax :
public Iterator Iterator() Parameter ---------- Return iterator over the deque.
9. 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.
10. element() : java.util.ArrayDeque.element() returns element at the head of the deque
Syntax :
public E element() Parameter ------ Return head element of the deque
11. getFirst: java.util.ArrayDeque.getFirst()returns first element of the deque
Syntax :
public E getFirst() Parameter ------ Return head element of the deque
12. getLast: java.util.ArrayDeque.getLast()returns last element of the deque
Syntax :
public E getLast() Parameter ------ Return last element of the deque
13. 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
14. 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
// Java code explaining the use of ArrayDeque class methods // add(), addFirst(), addLast(), clear(), size(), contains() // descendingIterator(), element(), getFirst(), isEmpty(), // toArray, Iterator(), getLast() import java.util.*; public class NewClass { public static void main(String[] args) { // Initializing 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); } // contains() 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 Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar 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.