The perfect place for easy learning...

Java Programming

×

Topics List


Java ArrayDeque Class





The ArrayDeque class is a part of java collection framework. It is available inside the java.util package. The ArrayDeque class extends AbstractCollection class and implements Deque, Cloneable, and Serializable interfaces.

The elements of ArrayDeque are organized as the elements of double ended queue data structure. The ArrayDeque is a special kind of array that grows and allows users to add or remove an element from both the sides of the queue.

The ArrayDeque class is used to create a dynamic double ended queue of elements that can grow or shrunk as needed.

🔔 The ArrayDeque is a child class of AbstractCollection

🔔 The ArrayDeque implements interfaces like Deque, Cloneable, and Serializable.

🔔 The ArrayDeque allows to store duplicate data values, but not null values.

🔔 The ArrayDeque maintains the order of insertion.

🔔 The ArrayDeque allows to add and remove elements at both the ends.

🔔 The ArrayDeque is faster than LinkedList and Stack.

ArrayDeque class declaration

The ArrayDeque class has the following declaration.

Example
public class ArrayDeque<E> extends AbstractCollection<E> implements Deque<E>, Cloneable, Serializable  

ArrayDeque class constructors

The PriorityQueue class has the following constructors.

  • ArrayDeque( ) - Creates an empty ArrayDeque with the default initial capacity (16).
  • ArrayDeque(Collection c) - Creates a ArrayDeque with given collection of elements.
  • ArrayDeque(int initialCapacity) - Creates an empty ArrayDeque with the specified initial capacity.

Operations on ArrayDeque

The ArrayDeque class allow us to perform several operations like adding, accesing, deleting, updating, looping, etc. Let's look at each operation with examples.

Adding Items

The ArrayDeque class has the following methods to add items.

  • boolean add(E element) - Appends given element to the ArrayDeque.
  • boolean addAll(Collection c) - Appends given collection of elements to the ArrayDeque.
  • void addFirst(E element) - Adds given element at front of the ArrayDeque.
  • void addLast(E element) - Adds given element at end of the ArrayDeque.
  • boolean offer(E element) - Adds given element at end of the ArrayDeque.
  • boolean offerFirst(E element) - Adds given element at front of the ArrayDeque.
  • boolean offerLast(E element) - Adds given element at end of the ArrayDeque.
  • void push(E element) - Adds given element at front of the ArrayDeque.

Let's consider an example program to illustrate adding items to the ArrayDeque.

Example
import java.util.*;

public class ArrayDequeExample {

	public static void main(String[] args) {

		ArrayDeque deque = new ArrayDeque();
		ArrayDeque anotherDeque = new ArrayDeque();

		deque.add(10);
		deque.addFirst(5);
		deque.addLast(15);
		deque.offer(20);
		deque.offerFirst(10);
		deque.offerLast(30);
		
		System.out.println("\nDeque is\n" + deque);
		
		anotherDeque.addAll(deque);
		
		System.out.println("\nanotherDeque is\n" + anotherDeque);
		
		anotherDeque.push(40);
		
		System.out.println("\nanotherDeque after push(40) is\n" + anotherDeque);
	}

}

ArrayDeque class in java

Accessing Items

The ArrayDeque class has the following methods to access items.

  • E element( ) - Returns the first element from the invoking ArrayDeque.
  • E getFirst( ) - Returns the first element from the invoking ArrayDeque.
  • E getLast( ) - Returns the last element from the invoking ArrayDeque.
  • E peek( ) - Returns the first element from the invoking ArrayDeque, returns null if this queue is empty.
  • E peekFirst( ) - Returns the first element from the invoking ArrayDeque, returns null if this queue is empty.
  • E peekLast( ) - Returns the last element from the invoking ArrayDeque, returns null if this queue is empty.

Let's consider an example program to illustrate accessing items from the ArrayDeque.

Example
import java.util.*;

public class ArrayDequeExample {

	public static void main(String[] args) {

		ArrayDeque deque = new ArrayDeque();

		for(int i = 1; i <= 10; i++)
			deque.add(i);
		
		System.out.println("\nDeque is\n" + deque);		
		
		System.out.println("\nelement() - " + deque.element());
		
		System.out.println("\ngetFirst() - " + deque.getFirst());
		
		System.out.println("\ngetLast() - " + deque.getLast());
		
		System.out.println("\npeek() - " + deque.peek());
		
		System.out.println("\npeekFirst() - " + deque.peekFirst());
		
		System.out.println("\npeekLast() - " + deque.peekLast());
	}

}

ArrayDeque class in java

Updating Items

The ArrayDeque class has no methods to update or change items.

Removing Items

The ArrayDeque class has the following methods to remove items.

  • E remove( ) - Removes the first element from the invoking ArrayDeque.
  • E removeFirst( ) - Removes the first element from the invoking ArrayDeque.
  • E removeLast( ) - Removes the last element from the invoking ArrayDeque.
  • boolean remove(Object o) - Removes the specified element from the invoking ArrayDeque.
  • boolean removeFirstOccurrence(Object o) - Removes the first occurrence of the specified element in this ArrayDeque.
  • boolean removeLastOccurrence(Object o) - Removes the last occurrence of the specified element in this ArrayDeque.
  • boolean removeIf(Predicate p) - Removes all of the elements of ArrayDeque collection that satisfy the given predicate.
  • boolean retainAll(Collection c) - Removes all of the elements of ArrayDeque collection except specified collection of elements.
  • E poll( ) - Removes the first element from the ArrayDeque, and returns null if the list is empty.
  • E pollFirst( ) - Removes the first element from the ArrayDeque, and returns null if the list is empty.
  • E pollLast( ) - Removes the last element from the ArrayDeque, and returns null if the list is empty.
  • E pop( ) - Removes the first element from the ArrayDeque.
  • void clear( ) - Removes all the elements from the PriorityQueue.

Let's consider an example program to illustrate removing items from the ArrayDeque.

Example
import java.util.*;

public class ArrayDequeExample {

	public static void main(String[] args) {

		ArrayDeque deque = new ArrayDeque();
        
		for(int i = 1; i <= 10; i++)
			deque.add(i);	
            
		System.out.println("\nDeque is\n" + deque);		
		
		deque.remove();
		System.out.println("\nDeque after remove()\n" + deque);
		
		deque.removeFirst();
		System.out.println("\nDeque after removeFirst()\n" + deque);
		
		deque.removeLast();
		System.out.println("\nDeque after removeLast()\n" + deque);
		
		deque.remove(5);
		System.out.println("\nDeque after remove(5)\n" + deque);
		
		deque.poll();
		System.out.println("\nDeque after poll()\n" + deque);
		
		deque.pollFirst();
		System.out.println("\nDeque after pollFirst()\n" + deque);
		
		deque.pollLast();
		System.out.println("\nDeque after pollLast()\n" + deque);
		
		deque.pop();
		System.out.println("\nDeque after pop()\n" + deque);
		
		deque.clear();
		System.out.println("\nDeque after clear()\n" + deque);
	}
}

ArrayDeque class in java

Other utility methods

The ArrayDeque class has the following methods to work with elements of it.

  • int size( ) - Returns the total number of elements in the invoking ArrayDeque.
  • boolean isEmpty( ) - Returns true if the ArrayDeque is empty otherwise returns false.
  • ArrayDeque clone( ) - Returns a copy of the invoking ArrayDeque.
  • boolean contains(Object element) - Returns true if the ArrayDeque contains given element otherwise returns false.
  • boolean containsAll(Collection c) - Returns true if the ArrayDeque contains given collection of elements otherwise returns false.
  • boolean equals(Object o) - Compares the specified object with invoking ArrayDeque collection for equality.
  • int hashCode( ) - Returns the hash code of the invoking ArrayDeque.
  • Object[ ] toArray( ) - Returns an array of Object instances that contains all the elements from invoking ArrayDeque.
  • Spliterator spliterator( ) - Creates spliterator over the elements in a ArrayDeque.
  • Iterator iterator( ) - Returns an iterator over the elements in the ArrayDeque. The iterator does not return the elements in any particular order.

Let's consider an example program to illustrate other utility methods of the ArrayDeque.

Example
import java.util.*;

public class ArrayDequeExample {

	public static void main(String[] args) {

		ArrayDeque deque = new ArrayDeque();
		
		for(int i = 1; i <= 10; i++)
			deque.add(i);	
		
		System.out.println("\nDeque is\n" + deque);		
		
		System.out.println("\nsize() - " + deque.size());
		
		System.out.println("\nisEmpty() - " + deque.isEmpty());
		
		System.out.println("\ncontains(6) - " + deque.contains(6));
		
		System.out.println("\nhashCode() - " + deque.hashCode());
		
		System.out.println("\nequals(6) - " + deque.equals(6));
	}
}

ArrayDeque class in java

Consolidated list of methods

The following table providess a consolidated view of all methods of ArrayDeque.

Method Description
boolean add(E element) Appends given element to the ArrayDeque.
boolean addAll(Collection c) Appends given collection of elements to the ArrayDeque.
void addFirst(E element) Inserts the given element at front of the ArrayDeque.
void addLast(E element) Inserts the given element at end of the ArrayDeque.
boolean offer(E element) Inserts the given element at end of the ArrayDeque.
boolean offerFirst(E element) Inserts the given element at front of the ArrayDeque.
boolean offerLast(E element) Inserts the given element at end of the ArrayDeque.
void push(E element) Inserts the given element at front of the ArrayDeque.
E element( ) Returns the first element from the ArrayDeque.
E getFirst( ) Returns the first element from the ArrayDeque.
E getLast( ) Returns the last element from the ArrayDeque.
E peek( ) Returns the first element from the invoking ArrayDeque, returns null if this queue is empty.
E peekFirst( ) Returns the first element from the invoking ArrayDeque, returns null if this queue is empty.
E peekLast( ) Returns the last element from the invoking ArrayDeque, returns null if this queue is empty.
E remove() Removes the first element from the invoking ArrayDeque.
E removeFirst() Removes the first element from the invoking ArrayDeque.
E removeLast() Removes the last element from the invoking ArrayDeque.
boolean remove(Object element) Removes the first occurence of the given element from the invoking ArrayDeque.
boolean removeFirstOccurrence(Object element) Removes the first occurrence of the specified element in this ArrayDeque.
boolean removLastOccurrence(Object element) Removes the last occurrence of the specified element in this ArrayDeque.
boolean removeIf(Predicate p) Removes all of the elements of the ArrayDeque collection that satisfy the given predicate.
boolean retainAll(Collection c) Removes all the elements except those are in the specified collection from the invoking ArrayDeque.
E poll( ) Removes the first element from the ArrayDeque, and returns null if the ArrayDeque is empty.
E pollFirst( ) Removes the first element from the ArrayDeque, and returns null if the ArrayDeque is empty.
E pollLast( ) Removes the last element from the ArrayDeque, and returns null if the ArrayDeque is empty.
E pop() Removes the first element from the invoking ArrayDeque.
void clear( ) Removes all the elements from the ArrayDeque.
int size( ) Returns the total number of elements in the invoking ArrayDeque.
boolean isEmpty( ) Returns true if the ArrayDeque is empty otherwise returns false.
boolean equals( ) Compares the specified object with invoking ArrayDeque collection for equality.
boolean contains(Object element) Returns true if the ArrayDeque contains given element otherwise returns false.
boolean containsAll(Collection c) Returns true if the ArrayDeque contains all elements of given collection otherwise returns false.
int hashCode( ) Returns the hash code of the invoking ArrayDeque.
Object[ ] toArray( ) Returns an array of Object instances that contains all the elements from invoking ArrayDeque.
Spliterator spliterator( ) Creates spliterator over the elements in a ArrayDeque.
Iterator iterator( ) Returns an iterator over the elements in the ArrayDeque. The iterator does not return the elements in any particular order.