30% Therapy – 40% Practice – 30% Work project

Java ArrayDeque Class



Introduction

The Java ArrayDeque class provides resizable-array and implements the Deque interface. Following are the important points about Array Deques −

  • Array deques have no capacity restrictions so they grow as necessary to support usage.

  • They are not thread-safe; in the absence of external .

  • They do not support concurrent access by .

  • Null elements are prohibited in the array deques.

  • They are faster than Stack and LinkedList.

This class and its iterator implement all of the optional methods of the and interfaces.

ArrayDeque Class Declaration

Following is the declaration for java.util.ArrayDeque class −

public class ArrayDeque<E>
   extends AbstractCollection<E>
   implements Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, Queue<E>

Here <E> represents an Element, which could be any class. For example, if you”re building an array deque of Integers then you”d initialize it as −

Deque<Integer> objDeque = new ArrayDeque<>();  

ArrayDeque Class Constructors

Sr.No.

Constructor & Description

1

ArrayDeque()

This constructor is used to create an empty array deque with an initial capacity sufficient to hold 16 elements.

2

ArrayDeque(Collection<? extends E> c)

This constructor is used to create a deque containing the elements of the specified collection.

3

ArrayDeque(int numElements)

This constructor is used to create an empty array deque with an initial capacity sufficient to hold the specified number of elements.

ArrayDeque Class Methods

Sr.No.

Method & Description

1

This method inserts the specified element at the end of this deque.

2

This method adds all of the elements in the specified collection at the end of this deque, as if by calling addLast(E) on each one, in the order that they are returned by the collection”s iterator.

3

This method inserts the specified element at the front of this deque.

4

This method inserts the specified element at the end of this deque.

5

This method removes all of the elements from this deque.

6

This method returns a copy of this deque.

7

This method returns true if this deque contains the specified element.

8

This method retrieves, but does not remove, the head of the queue represented by this deque.

9

This method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

10

This method retrieves, but does not remove, the first element of this deque.

11

This method retrieves, but does not remove, the last element of this deque.

12

This method returns true if this deque contains no elements.

13

This method returns an iterator over the elements in this deque.

14

This method inserts the specified element at the end of this deque.

15

This method inserts the specified element at the front of this deque.

16

This method inserts the specified element at the end of this deque.

17

This method retrieves, but does not remove, the head of the queue represented by this deque, or returns null if this deque is empty.

18

This method retrieves and removes the head of the queue represented by this deque, or returns null if this deque is empty.

19

This method retrieves and removes the first element of this deque, or returns null if this deque is empty.

20

This method retrieves and removes the last element of this deque, or returns null if this deque is empty.

21

This method pops an element from the stack represented by this deque.

22

This method pushes an element onto the stack represented by this deque.

23

This method retrieves and removes the head of the queue represented by this deque.

24

This method removes all of this collection”s elements that are also contained in the specified collection.

25

This method retrieves and removes the first element of this deque.

26

This method removes the first occurrence of the specified element in this deque.

27

This method removes all of the elements of this collection that satisfy the given predicate.

28

This method retrieves and removes the last element of this deque.

29

This method removes the last occurrence of the specified element in this deque.

30

This method retains only the elements in this collection that are contained in the specified collection (optional operation).

31

This method returns the number of elements in this deque.

32

This method creates a late-binding and fail-fast Spliterator over the elements in this deque.

33

This method returns an array containing all of the elements in this deque in proper sequence.

Adding and Removing Elements from an ArrayDeque Example

This example demonstrates how you can use an ArrayDeque Class.

// Importing classes
import java.util.ArrayDeque;
import java.util.Deque;

// Public Main Class
public class Main {
  public static void main(String[] args) {
    // The main() function
    Deque < Integer > objDeque = new ArrayDeque < > ();
    // Adding elements at first and last 
    objDeque.addFirst(15);
    objDeque.addLast(28);

    // Removing the elements
    int ele1 = objDeque.removeFirst();
    int ele2 = objDeque.removeLast();

    // Printing removed elements
    System.out.println("First removed element is : " + ele1);
    System.out.println("Last removed element is : " + ele2);
  }
}

Output

This will produce the following result −

First removed element is : 15
Last removed element is : 28
Translate »