ArrayDeque in Java

ArrayDeque in Java is a class belonging to the Java Collection framework. It extends the AbstractCollection class and implements the Deque, Cloneable, and Serializable interfaces.

ArrayDeque in java

The ArrayDeque class supplies the implementation of the Deque interface and offers a resizable array. It is also referred to as an Array Double Ended Queue, or Array Deck.

  • Array deques expand automatically, so there are no capacity restrictions.
  • Array deques are not thread-safe, which means ArrayDeque does not allow concurrent access by multiple threads.
  • Null elements are not permitted in an ArrayDeque.
  • The ArrayDeque class is faster than Stack and Deque.

Constructors of ArrayDeque in Java

  1. ArrayDeque(): This is the default constructor of the ArrayDeque class. It creates an empty ArrayDeque with an initial default capacity of 16.
  2. ArrayDeque(Collection<E> c): This constructor creates an ArrayDeque from the elements of the specified collection c.
  3. ArrayDeque(int initialCapacity): This constructor creates an empty ArrayDeque with the capacity you specify.

Methods of ArrayDeque in Java

  1. add(E e): Adds the given element to the Deque by appending it at the end. Its return type is boolean.
  2. addFirst(E e): Inserts the given element at the front of the Deque. Its return type is void.
  3. addLast(E e): Inserts the given element at the end of the Deque. Its return type is void.
  4. offer(E e): Adds the given element at the end of the Deque. Its return type is boolean.
  5. offerFirst(E e): Adds the given element at the front of the Deque. Its return type is boolean.
  6. offerLast(E e): Adds the given element at the end of the Deque. Its return type is boolean.
  7. push(): Pushes the given element onto the front of the Deque. Its return type is void.
  8. remove(): Removes and returns the first element of the Deque. Its return type is E.
  9. removeFirst(): Removes and returns the first element of the Deque. Its return type is E.
  10. removeLast(): Removes and returns the last element of the Deque. Its return type is E.
  11. removeFirstOccurrence(): Removes the first occurrence of the specified element from the Deque. Its return type is boolean.
  12. removeLastOccurrence(): Removes the last occurrence of the specified element from the Deque. Its return type is boolean.
  13. getFirst(): Returns the first element of the Deque. Its return type is E.
  14. getLast(): Returns the last element of the Deque. Its return type is E.
  15. peek(): Retrieves the first element of the Deque. Its return type is E.
  16. peekFirst(): Also retrieves the first element of the Deque. Its return type is E.
  17. peekLast(): Retrieves the last element of the Deque. Its return type is E.
  18. poll(): Removes and returns the first element of the Deque. Its return type is E.
  19. pollFirst(): Also removes and returns the first element of the Deque. Its return type is E.
  20. pollLast(): Removes and returns the last element of the Deque. Its return type is E.
  21. pop(): Also removes and returns the first element of the Deque. Its return type is E.
  22. element(): Returns the first element of the Deque without removing it from the list. Its return type is E.
  23. isEmpty(): Checks whether the Deque is empty or not. Its return type is boolean.
  24. clear(): Removes every element from the Deque. It returns nothing, so its return type is void.
  25. contains(Object o): Checks whether the given element is present in the ArrayList or not. Its return type is boolean.
  26. size(): Returns the number of elements currently present in the Deque.
  27. clone(): Returns a shallow copy of this Deque instance.
  28. iterator(): Returns an iterator over the elements of this Deque.
  29. descendingIterator(): Returns an iterator over the elements in descending order.
  30. Returns an array containing all of the elements of the ArrayList in the correct order.
// Java program to demonstrate few functions of 
// ArrayDeque in Java 
import java.util.ArrayDeque; 
public class ExampleOfArrayDeque 
{ 
	public static void main(String[] args) 
	{ 
		ArrayDeque<Integer> numbers = new ArrayDeque<Integer>(10); 
		numbers.add(10); 
		numbers.add(20); 
		numbers.add(30); 
		numbers.add(40); 
		numbers.add(50); 
		for (Integer element : numbers) 
		{ 
			System.out.println("Element : " + element); 
		} 
		// remove() method : to get head 
		System.out.println("Head element remove : " + numbers.remove()); 
		System.out.println("The final array is: "+numbers); 
	} 
}

Output: Element : 10
Element : 20
Element : 30
Element : 40
Element : 50
Head element remove : 10
The final array is: [20, 30, 40, 50]

Leave a Comment

Because ArrayDeque implements the Deque interface, you can use it as a double-ended queue and add or remove elements from either end. Its constructors let you start with the default capacity, a chosen initial capacity, or the contents of an existing collection. Keep in mind that it grows automatically, rejects null elements, and is not thread-safe.