English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about the ArrayDeque class and its methods with the help of examples. We will also learn to implement a stack using ArrayDeque.
In Java, we can use the ArrayDeque class to implement the queue and double-ended queue data structures using arrays.
The ArrayDeque class implements these two interfaces:
To create an ArrayDeque double-ended queue, we must import the java.util.ArrayDeque package.
This is the method we can use to create a ArrayDeque double-ended queue in Java:
ArrayDeque<Type> animal = new ArrayDeque<>();
Here, Type represents the type of the ArrayDeque double-ended queue. For example,
//Create an ArrayDeque of string type ArrayDeque<String> animals = new ArrayDeque<>(); //Create an ArrayDeque of integer type ArrayDeque<Integer> age = new ArrayDeque<>();
The ArrayDeque class provides all the methods existing in the Queue and Deque interfaces.
1.Use add(), addFirst(), and addLast() to add elements
add() - Inserts the specified element at the end of the ArrayDeque double-ended queue
addFirst() -Insert the specified element at the beginning of the ArrayDeque double-ended queue
addLast() - Insert the specified content at the end of the ArrayDeque double-ended queue (equivalent to add())
Note:If the ArrayDeque double-ended queue is full, all these methods add(), addFirst(), and addLast() will throw an IllegalStateException.
For example,
import java.util.ArrayDeque; class Main { public static void main(String[] args) { ArrayDeque<String> animals = new ArrayDeque<>(); //Use add () animals.add("Dog"); //Use addFirst () Use addFirst() //Use addLast() animals.addLast("Horse"); System.out.println("ArrayDeque: ", + animals); } }
Output Result
ArrayDeque: [Cat, Dog, Horse]
2Use offer(), offerFirst(), and offerLast() to insert elements
offer() - Inserts the specified element at the end of the ArrayDeque double-ended queue
offerFirst() - Inserts the specified element at the beginning of the ArrayDeque double-ended queue
offerLast() - Inserts the specified element at the end of the ArrayDeque double-ended queue
Note: offer(), offerFirst(), and offerLast() return true if the element is successfully inserted; otherwise, return. If the ArrayDeque double-ended queue is full, these methods return false.
For example,
import java.util.ArrayDeque; class Main { public static void main(String[] args) { ArrayDeque<String> animals = new ArrayDeque<>(); //Using offer() animals.offer("Dog"); //Using offerFirst() animals.offerFirst("Cat"); //Using offerLast() animals.offerLast("Horse"); System.out.println("ArrayDeque: ", + animals); } }
Output Result
ArrayDeque: [Cat, Dog, Horse]
1Use getFirst() and getLast() to access elements
getFirst() - Returns the first element of the ArrayDeque double-ended queue
getLast() - Returns the last element of the ArrayDeque double-ended queue
Note:If the ArrayDeque double-ended queue is empty, getFirst() and getLast() throw NoSuchElementException.
For example,
import java.util.ArrayDeque; class Main { public static void main(String[] args) { ArrayDeque<String> animals = new ArrayDeque<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayDeque: ", + animals); // Get the first element String firstElement = animals.getFirst(); System.out.println("The first element: ") + firstElement); //Get the last element String lastElement = animals.getLast(); System.out.println("The last element: ") + lastElement); } }
Output Result
ArrayDeque: [Dog, Cat, Horse] First element: Dog Last element: Horse
2Use peek(), peekFirst(), and peekLast() methods to access elements
peek() - Returns the first element of the ArrayDeque double-ended queue
peekFirst() - Returns the first element of the ArrayDeque double-ended queue (equivalent to peek())
peekLast() - Returns the last element of the ArrayDeque double-ended queue
For example,
import java.util.ArrayDeque; class Main { public static void main(String[] args) { ArrayDeque<String> animals = new ArrayDeque<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayDeque: ", + animals); //Using peek() String element = animals.peek(); System.out.println("The head element: ") + element); //Using peekFirst() String firstElement = animals.peekFirst(); System.out.println("The first element: ") + firstElement); //Using peekLast String lastElement = animals.peekLast(); System.out.println("The last element: ") + lastElement); } }
Output Result
ArrayDeque: [Dog, Cat, Horse] Head Element: Dog First element: Dog Last element: Horse
Note:If the ArrayDeque deque is empty, peek(), peekFirst(), and getLast() throw NoSuchElementException.
1.Use remove(), removeFirst(), and removeLast() methods to delete elements
remove() - Return and delete an element from the first element of the ArrayDeque deque
remove(element) - Return and delete the specified element from the head of the ArrayDeque deque
removeFirst() - Return and delete the first element from the ArrayDeque deque (equivalent to remove())
removeLast() - Return and delete the last element from the ArrayDeque deque
Note:If the ArrayDeque deque is empty, the remove(), removeFirst(), and removeLast() methods will throw an exception. In addition, if the element is not found, remove(element) will throw an exception.
For example,
import java.util.ArrayDeque; class Main { public static void main(String[] args) { ArrayDeque<String> animals = new ArrayDeque<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Cow"); animals.add("Horse"); System.out.println("ArrayDeque: ", + animals); //Use remove() String element = animals.remove(); System.out.println("Delete Element: ") + element); System.out.println("New ArrayDeque: ", + animals); //Use removeFirst() String firstElement = animals.removeFirst(); System.out.println("Remove the first element: ", + firstElement); //Use removeLast() String lastElement = animals.removeLast(); System.out.println("Remove the last element: ", + lastElement); } }
Output Result
ArrayDeque: [Dog, Cat, Cow, Horse] Remove Element: Dog New ArrayDeque: [Cat, Cow, Horse] Remove the first element: Cat Remove the last element: Horse
2.Use poll(), pollFirst(), and pollLast() methods to delete elements
poll() - Return and delete the first element of the ArrayDeque deque
pollFirst() - Return and delete the first element of the ArrayDeque deque (equivalent to poll())
pollLast() - Return and delete the last element of the ArrayDeque deque
Note:If the ArrayDeque deque is empty, if the element is not found, poll(), pollFirst(), and pollLast() return null.
For example,
import java.util.ArrayDeque; class Main { public static void main(String[] args) { ArrayDeque<String> animals = new ArrayDeque<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Cow"); animals.add("Horse"); System.out.println("ArrayDeque: ", + animals); //Use poll() String element = animals.poll(); System.out.println("Delete Element: ") + element); System.out.println("New ArrayDeque: ", + animals); //Use pollFirst() String firstElement = animals.pollFirst(); System.out.println("Remove the first element: ", + firstElement); //Using pollLast() String lastElement = animals.pollLast(); System.out.println("Remove the last element: ", + lastElement); } }
Output Result
ArrayDeque: [Dog, Cat, Cow, Horse] Remove Element: Dog New ArrayDeque: [Cat, Cow, Horse] Remove the first element: Cat Remove the last element: Horse
3.Remove element: Using clear() method
To remove all elements from the ArrayDeque deque, we use the clear() method. For example,
import java.util.ArrayDeque; class Main { public static void main(String[] args) { ArrayDeque<String> animals = new ArrayDeque<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("ArrayDeque: ", + animals); //Using clear() animals.clear(); System.out.println("New ArrayDeque: ", + animals); } }
Output Result
ArrayDeque: [Dog, Cat, Horse] New ArrayDeque: []
iterator() - Returns an iterator that can be used to traverse the ArrayDeque deque
descendingIterator() -Returns an iterator that can be used to traverse the ArrayDeque deque in reverse order
To use these methods, we must import the java.util.Iterator package. For example,
import java.util.ArrayDeque; import java.util.Iterator; class Main { public static void main(String[] args) { ArrayDeque<String> animals = new ArrayDeque<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.print("ArrayDeque: "); //Using iterator() Iterator<String> iterate = animals.iterator(); while(iterate.hasNext()) { System.out.print(iterate.next()); System.out.print(", "); } System.out.print("\nReverse ArrayDeque: "); //Use descendingIterator() Iterator<String> desIterate = animals.descendingIterator(); while(desIterate.hasNext()) { System.out.print(desIterate.next()); System.out.print(", "); } } }
Output Result
ArrayDeque: [Dog, Cat, Horse] Reverse ArrayDeque: [Horse, Cat, Dog]
Method | Content description |
---|---|
element() | Return an element from the head of the ArrayDeque deque. |
contains(element) | Search for the specified element in the ArrayDeque deque. Return true if the element is found, otherwise return false. |
size() | Return the length of the ArrayDeque deque. |
toArray() | Convert the ArrayDeque deque to an array and return it. |
clone() | Create a copy of the ArrayDeque deque and return it. |
To implement a stack in JavaLIFO (Last In, First Out)Stack, it is recommended to useStack classUsing a deque. The ArrayDeque is faster than the Stack class.
ArrayDeque provides the following methods that can be used to implement a stack.
push() - Add an element to the top of the stack
peek() - Return an element from the top of the stack
pop() - Return and remove the element from the top of the stack
For example,
import java.util.ArrayDeque; class Main { public static void main(String[] args) { ArrayDeque<String> stack = new ArrayDeque<>(); //Add the element to the stack stack.push("Dog"); stack.push("Cat"); stack.push("Horse"); System.out.println("Stack: ", + stack); //Accessing element from the top of the stack String element = stack.peek(); System.out.println("Accessing element: ", + element); //Delete element from the top of the stack String remElement = stack.pop(); System.out.println("Delete element: ", + remElement); } }
Output Result
Stack: [Horse, Cat, Dog] Access Element: Horse Delete Element: Horse
ArrayDeque andJava's LinkedListImplemented the Deque interface. However, there are some differences between them.
LinkedList supports empty elements, while ArrayDeque does not.
Each node in the linked list contains a link to another node. This is why LinkedList requires more storage space than ArrayDeque.
If you need to implement a queue or deque data structure, ArrayDeque may be faster than LinkedList.