Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

ArrayDeque

The ArrayDeque is a class available in the java.util package. It extends the java.util.AbstractCollection class and implements the java.util.Deque, java.lang.Cloneable, and java.io.Serializable interfaces.

Important point of java.util.ArrayDeque:

1. We use ArrayDeque to get a resizable array, and this array functions as a double-ended queue, allowing us to add and remove elements from both sides. It provides constant time complexity for adding and removing elements.
2. ArrayDeque allows the addition of different types of elements.
3. Duplicity is allowed in ArrayDeque.
4. We can iterate through ArrayDeque using an Iterator.
5. We can't iterate through ArrayDeque using ListIterator and Enumeration.
6. Because the java.util.ArrayDeque class implements the java.util.Cloneable and java.io.Serializable interfaces, it is eligible for cloning and serialization.

Important Constructor of java.util.ArrayDeque:

1. public java.util.ArrayDeque(): It creates an empty ArrayDeque with an initial capacity of 16.

2. public java.util.ArrayDeque(int): It creates an empty ArrayDeque with the specified capacity.

3. public java.util.ArrayDeque(java.util.Collection): It creates an ArrayDeque that contains all the elements of the specified collection, and the order of the elements will be the same as in the specified collection.

            
importjava.util.ArrayDeque;
importjava.util.ArrayList;
publicclass JTC {
   publicstaticvoid main(String[] args) {
      // Creating empty ArrayDeque using default constructor of java.util.ArrayDeque class and Initial Capacity :- 16.
      ArrayDequedeque = newArrayDeque();
      System.out.println("deque :- " + deque);
      System.out.println("Size :- " + deque.size());
      // Creating empty ArrayDeque using java.util.ArrayDeque(int) constructor and Initial Capacity :- 56 . 
      ArrayDequedeque1 = newArrayDeque(56);
      System.out.println("deque1 :- " + deque1);
      System.out.println("Size :- " + deque1.size());
      // Creating an ArrayDeque which contains all the elements of specified collection which is ArrayList.
      ArrayListlist = newArrayList();
      list.add(101);
      list.add("Hello");
      list.add('A');
      list.add(true);
      list.add(12.34);
      ArrayDequedeque2 = newArrayDeque(list);
      System.out.println("deque2 :- " + deque2);
      System.out.println("Size :- " + deque2.size());
   }
}
    deque :- []
    Size :- 0
    deque1 :- []
    Size :- 0
    deque2 :- [101, Hello, A, true, 12.34]
    Size :- 5
            

Important Methods of java.util.ArrayDeque class:

1. publicboolean add(Object):- It inserts elements at the end of the deque.

2. public void addFirst(Object) :- It inserts elements at the front of the deque.

3. public void addLast(Object) :- It inserts elements at the last of the deque.

4. public Object removeFirst() :- It retrieves and removes the element from the first position of the ArrayDeque, throwing a NoSuchElementException if the ArrayDeque is empty.

5. public Object removeLast() :- It retrieves and removes the element from the last position of the ArrayDeque, throwing a NoSuchElementException if the ArrayDeque is empty.

6. public Object peekLast() :- It retrieves the element from the last position of the ArrayDeque without removing the element, returning null if the ArrayDeque is empty.

7. public E peekFirst() :- It retrieves the element from the first position of the ArrayDeque without removing it, returning null if the ArrayDeque is empty.

8. publicjava.util.IteratordescendingIterator() :- It returns an iterator over the elements in this ArrayDeque in reverse sequential order.