Java – ArrayDeque


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 synchronization.

  • They do not support concurrent access by multiple threads.

  • 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 Collection and Iterator 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

boolean add(E e)

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

2

boolean addAll​(Collection<? extends E> c)

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

void addFirst(E e)

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

4

void addLast(E e)

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

5

void clear()

This method removes all of the elements from this deque.

6

ArrayDeque<E> clone()

This method returns a copy of this deque.

7

boolean contains(Object o)

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

8

E element()

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

9

void forEach​(Consumer<? super E> action)

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

E getFirst()

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

11

E getLast()

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

12

boolean isEmpty()

This method returns true if this deque contains no elements.

13

Iterator<E> iterator()

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

14

boolean offer(E e)

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

15

boolean offerFirst(E e)

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

16

boolean offerLast(E e)

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

17

E peek()

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

E poll()

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

19

E pollFirst()

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

20

E pollLast()

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

21

E pop()

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

22

void push(E e)

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

23

E remove()

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

24

boolean removeAll​(Collection<?> c)

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

25

E removeFirst()

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

26

boolean removeFirstOccurrence(Object o)

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

27

boolean removeIf​(Predicate<? super E> filter)

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

28

E removeLast()

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

29

boolean removeLastOccurrence(Object o)

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

30

boolean retainAll​(Collection<?> c)

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

31

int size()

This method returns the number of elements in this deque.

32

Spliterator<E> spliterator()

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

33

object[] toArray()

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 Java 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

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *