ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both the sides of the queue. Few important features of ArrayDeque are as follows:
- Array deques have no capacity restrictions and they grow as necessary to support usage.
- They are not thread-safe which means that in the absence of external synchronization, ArrayDeque does not support concurrent access by multiple threads.
- Null elements are prohibited in the ArrayDeque.
- ArrayDeque class is likely to be faster than Stack when used as a stack.
- ArrayDeque class is likely to be faster than LinkedList when used as a queue.
Declaration:
public class ArrayDeque Element extends AbstractCollection implements DequeElement, Cloneable, Serializable
Here, Element refers to the element which can refer to any class, such as Integer or String class.
Constructors in ArrayDeque:
- ArrayDeque(): Used to create an empty ArrayDeque and by default holds an initial capacity to hold 16 elements.
- ArrayDeque(Collection c): Used to create an ArrayDeque containing all the elements same as that of the specified collection.
- ArrayDeque(int numofElements): Used to create an empty ArrayDeque wand holds the capacity to contain a specified number of elements.
Example:
// Java program to demonstrate few functions of // ArrayDeque in Java import java.util.*; public class ArrayDequeDemo { public static void main(String[] args) { // Intializing an deque Deque<Integer> de_que = new ArrayDeque<Integer>( 10 ); // add() method to insert de_que.add( 10 ); de_que.add( 20 ); de_que.add( 30 ); de_que.add( 40 ); de_que.add( 50 ); for (Integer element : de_que) { System.out.println( "Element : " + element); } System.out.println( "Using clear() " ); // clear() method de_que.clear(); // addFirst() method to insert at start de_que.addFirst( 564 ); de_que.addFirst( 291 ); // addLast() method to insert at end de_que.addLast( 24 ); de_que.addLast( 14 ); System.out.println( "Above elements are removed now" ); // Iterator() : System.out.println( "Elements of deque using Iterator :" ); for (Iterator itr = de_que.iterator(); itr.hasNext();) { System.out.println(itr.next()); } // descendingIterator() : to reverse the deque order System.out.println( "Elements of deque in reverse order :" ); for (Iterator dItr = de_que.descendingIterator(); dItr.hasNext();) { System.out.println(dItr.next()); } // element() method : to get Head element System.out.println( "
Head Element using element(): " + de_que.element()); // getFirst() method : to get Head element System.out.println( "Head Element using getFirst(): " + de_que.getFirst()); // getLast() method : to get last element System.out.println( "Last Element using getLast(): " + de_que.getLast()); // toArray() method : Object[] arr = de_que.toArray(); System.out.println( "
Array Size : " + arr.length); System.out.print( "Array elements : " ); for ( int i= 0 ; i<arr.length ; i++) System.out.print( " " + arr[i]); // peek() method : to get head System.out.println( "
Head element : " + de_que.peek()); // poll() method : to get head System.out.println( "Head element poll : " + de_que.poll()); // push() method : de_que.push( 265 ); de_que.push( 984 ); de_que.push( 2365 ); // remove() method : to get head System.out.println( "Head element remove : " + de_que.remove()); System.out.println( "The final array is: " +de_que); } } |
Output:
Element : 10 Element : 20 Element : 30 Element : 40 Element : 50 Using clear() Above elements are removed now Elements of deque using Iterator : 291 564 24 14 Elements of deque in reverse order : 14 24 564 291 Head Element using element(): 291 Head Element using getFirst(): 291 Last Element using getLast(): 14 Array Size : 4 Array elements : 291 564 24 14 Head element : 291 Head element poll : 291 Head element remove : 2365 The final array is: [984, 265, 564, 24, 14]
Methods in ArrayDeque:
- add(Element e) : The method inserts particular element at the end of the deque.
- addFirst(Element e) : The method inserts particular element at the start of the deque.
- addLast(Element e) : The method inserts particular element at the end of the deque. It is similiar to add() method
- clear() : The method removes all deque elements.
- size() : The method returns the no. of elements in deque.
- clone() : The method copies the deque.
- contains(Obj) : The method checks whether a deque contains the element or not
- Iterator() : The method returns an iterator over the deque.
- descendingIterator() : The method returns a reverse order iterator over the deque
- element() : The method returns element at the head of the deque
- getFirst(): The method returns first element of the deque
- getLast(): The method returns last element of the deque
- isEmpty(): The method checks whether the deque is empty or not.
- toArray(): The method returns array having the elements of deque.
- offer(Element e) : The method inserts element at the end of deque.
- offerFirst(Element e) : The method inserts element at the front of deque.
- offerLast(Element e) : The method inserts element at the end of deque.
- peek() : The method returns head element without removing it.
- peekFirst() : The method returns first element without removing it.
- peekLast() : The method returns last element without removing it.
- poll() : The method returns head element and also removes it
- pollFirst() : The method returns first element and also removes it
- pollLast() : The method returns last element and also removes it
- pop() : The method pops out an element for stack repesented by deque
- push(Element e) : The method pushes an element onto stack repesented by deque
- remove() : The method returns head element and also removes it
- removeFirst() : The method returns first element and also removes it
- removeLast() : The method returns last element and also removes it
- removeFirstOccurrence(Obj) : The method removes the element where it first occur in the deque.
- removeLastOccurrence(Obj) : The method removes the element where it last occur in the deque.
Related Articles:
leave a comment
0 Comments