ArrayDeque in Java is a class that provides the facility of using deque and resizable array. it extends the AbstractCollection class and implements the Deque interface. ArrayDeque is also known as Array Double Ended Queue that allows the user to add or remove an element from both sides of the Queue. ArrayDeque creates a dynamic array which has no capacity restriction.
2. We can add or remove an element from both sides of the queue.
3. ArrayDeque has no capacity restriction.
4. Null elements are not allowed.
5. Not thread safe, in the absence of external synchronization.
6. ArrayDeque uses both stack and queue When it is used as stack it is faster than the Stack, and when it used queue it is faster than the LinkedList.
7. It adds no methods of its own.
Output:
You may enjoy these related posts:
How to find the length of ArrayList?
Difference between ArrayList and Vector in the collection
TreeSet in Java with Example
Comparable vs Comparator interface in Java with Example
NavigableSet in Java with Example
LinkedHashSet in Java Collection with an example
Points to remember about Java ArrayDeque
1. implements the Deque interface.2. We can add or remove an element from both sides of the queue.
3. ArrayDeque has no capacity restriction.
4. Null elements are not allowed.
5. Not thread safe, in the absence of external synchronization.
6. ArrayDeque uses both stack and queue When it is used as stack it is faster than the Stack, and when it used queue it is faster than the LinkedList.
7. It adds no methods of its own.
Constructors in Java ArrayDeque:
The ArrayDeque defines the following constructors:
1. ArrayDeque(): The first constructor creates an empty deque. Its initial capacity is 16.
2. ArrayDeque(int numOfElements): The second constructor is used to create an ArrayDeque that has the specified initial capacity.
3. ArrayDeque(Collection c): The third constructor is used to create an ArrayDeque that is initialized with the elements of the collection passed in c.
Java ArrayDeque Example:
import java.util.*;
class ArrayDequeExample{
public static void main(String args[]){
Deque<String> adq = new ArrayDeque<String>();
//adding the element in the array deque
adq.add("Ajay");
adq.add("Vijay");
adq.add("Rahul");
adq.add("Amit");
adq.add("Suresh");
System.out.println("ArrayDeque elements are: "+adq);
//insert at the first using addFirst()
adq.addFirst("Prashant");
//insert at the end using addLast()
adq.addLast("Sushant");
//displaying array deque elements
System.out.println("After addFirst() and addLast(): "+adq);
//size of the array deque
System.out.println("ArrayDeque size: " +adq.size());
//get the first element using getFirst()
System.out.println("First element is: " +adq.getFirst());
//get the last element using getLast()
System.out.println("Last element is: " +adq.getLast());
//remove all the elements
adq.clear();
System.out.println("After removing all elements: " +adq);
}
}
Output:
You may enjoy these related posts:
How to find the length of ArrayList?
Difference between ArrayList and Vector in the collection
TreeSet in Java with Example
Comparable vs Comparator interface in Java with Example
NavigableSet in Java with Example
LinkedHashSet in Java Collection with an example
ArrayDeque in Java with Example
Reviewed by Prashant Srivastava
on
March 17, 2019
Rating:
No comments: