Site icon JavaGoal

ArrayDeque in java

The ArrayDeque in java is a class that is a part of the Java Collection framework which extends AbstractCollection class and implements the Deque, Cloneable, and Serializable interface.

ArrayDeque class provides the implementation of the Deque interface and provides a resizable array. It is also known as Array Double Ended Queue or Array Deck

Constructors of ArrayDeque in java

  1. ArrayDeque(): It is a default constructor of ArrayDeque class. It is used to create an empty ArrayDeque with initial default capacity 16.
  2. ArrayDeque(Collection<E> c): ): It is used to create an ArrayDeque using the elements of the specified collection c.
  3. ArrayDeque (int initialCapacity): It is used to create an empty ArrayDeque with a specified capacity.

Methods of ArrayDeque in java

  1. add(E e): This method is used to add the given element in Deque. It appends the specified element at the end of Deque. Its return type is boolean.
  2. addFirst(E e): This method is used to add the given element at the beginning of Deque. Its return type is void.
  3. addLast(E e): This method is used to add the given element at the end of Deque. Its return type is void.
  4. offer(E e): This method is used to add the given element at the end of Deque. Its return type is boolean.
  5. offerFirst(E e): This method is used to add the given element at the beginning of Deque. Its return type is boolean.
  6. offerLast(E e): This method is used to add the given element at the end of Deque. Its return type is boolean.
  7. push(): This method is used to add the given element at the beginning of Deque. Its return type is void.
  8. remove(): This method is used removes and returns the first element from Deque. Its return type is E.
  9. removeFirst(): This method is used to remove and return the first element from Deque. Its return type is E.
  10. removeLast(): This method is used removes and returns the last element from Deque. Its return type is E.
  11. removeFirstOccurrence(): This method is used removes the first occurrence of the specified element from Deque. Its return type is boolean.
  12. removeLastOccurrence(): This method is used removes the last occurrence of the specified element from Deque. Its return type is boolean.
  13. getFirst(): This method is used to get the first element from Deque. Its return type is E.
  14. getLast(): This method is used to get the last element from Deque. Its return type is E.
  15. peek(): This method is used to get to the first element from Deque. Its return type is E.
  16. peekFirst(): This method is also used to get the first element from Deque. Its return type is E.
  17. peekLast(): This method is used get to the last element from Deque. Its return type is E.
  18. poll(): This method is used to removes and return the first element from Deque. Its return type is E.
  19. pollFirst(): This method is also used to removes and return the first element from Deque. Its return type is E.
  20. pollLast(): This method is used to removes and returns the last element from Deque. Its return type is E.
  21. pop(): This method is also used to removes and return the first element from Deque. Its return type is E.
  22. element(): This method is used to get the first element from Deque. But doesn’t remove the element from the list. Its return type is E.
  23. isEmpty(): This method is used to whether Deque is empty or not. Its return type is boolean.
  24. clear(): This method is used to remove all elements from the Deque. It doesn’t return anything, so its return type is void.
  25. contains(Object o): It is used to check whether the given element present in ArrayList or not. Its return type is boolean.
  26. size(): It is used to check the number of elements present in Deque.
  27. clone(): It is used to return a shallow copy of this Deque instance.
  28. iterator(): It is used to return an iterator over the elements in this Deque.
  29. descendingIterator(): It is used to return an iterator over the elements in descending order.
  30. This method is used to return an array containing all of the elements of the ArrayList in 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]

Exit mobile version