Java ArrayDeque Methods

The ArrayDeque class in Java is a resizable array implementation of the Deque interface. It provides a way to use a double-ended queue, allowing elements to be added or removed from both ends efficiently. This class is a part of the java.util package and is known for its flexibility and performance benefits compared to other collection classes.

This guide covers various methods available in the ArrayDeque class. Each method is described in simple terms to help beginners understand how to use them. These methods enable you to perform common operations such as adding, removing, and inspecting elements in the deque.

For more detailed information, please refer to the official Java SE Documentation.

Java ArrayDeque Methods

The table below contains various methods of the Java ArrayDeque class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
add() Adds the specified element to the end of this deque.
addAll() Adds all of the elements in the specified collection to the end of this deque.
addFirst() Inserts the specified element at the front of this deque.
addLast() Inserts the specified element at the end of this deque.
clear() Removes all of the elements from this deque.
clone() Returns a shallow copy of this deque.
contains() Returns true if this deque contains the specified element.
descendingIterator() Returns an iterator over the elements in this deque in reverse order.
element() Retrieves, but does not remove, the head of this deque.
forEach() Performs the given action for each element of the deque until all elements have been processed or the action throws an exception.
getFirst() Retrieves, but does not remove, the first element of this deque.
getLast() Retrieves, but does not remove, the last element of this deque.
isEmpty() Returns true if this deque contains no elements.
offer() Adds the specified element as the tail (last element) of this deque.
offerFirst() Inserts the specified element at the front of this deque.
iterator() Returns an iterator over the elements in this deque in proper sequence.
offerLast() Inserts the specified element at the end of this deque.
peek() Retrieves, but does not remove, the head of this deque, or returns null if this deque is empty.
peekFirst() Retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
peekLast() Retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
poll() Retrieves and removes the head of this deque, or returns null if this deque is empty.
pollFirst() Retrieves and removes the first element of this deque, or returns null if this deque is empty.
pollLast() Retrieves and removes the last element of this deque, or returns null if this deque is empty.
pop() Pops an element from the stack represented by this deque.
push() Pushes an element onto the stack represented by this deque.
remove() Removes the first occurrence of the specified element from this deque.
removeAll() Removes all of this deque's elements that are also contained in the specified collection.
removeFirst() Removes the first element of this deque.
removeFirstOccurrence() Removes the first occurrence of the specified element in this deque.
removeIf() Removes all of the elements of this deque that satisfy the given predicate.
removeLast() Removes the last element of this deque.
removeLastOccurrence() Removes the last occurrence of the specified element in this deque.
retainAll() Retains only the elements in this deque that are contained in the specified collection.
size() Returns the number of elements in this deque.
spliterator() Returns a late-binding and fail-fast Spliterator over the elements in this deque.
toArray() Returns an array containing all of the elements in this deque.

Comments