In Java, we can use the ArrayDeque class to implement queue and deque data structures using arrays. The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue.
There are some key points to remember about ArrayDeque :
The ArrayDeque class implements these two interfaces:
In order to create an array deque, we must import the java.util.ArrayDeque package.
// Creating String type ArrayDeque
ArrayDeque<String> animals = new ArrayDeque<>();
// Creating Integer type ArrayDeque
ArrayDeque<Integer> age = new ArrayDeque<>();
In order to create a ArrayDeque, we need to create an object of the ArrayDeque class. The ArrayDeque class consists of various constructors that allow the possible creation of the ArrayDeque. The following are the constructors available in this class.
Constructor | Description | Example |
---|---|---|
ArrayDeque() | This constructor is used to create an empty ArrayDeque and by default holds an initial capacity to hold 16 elements. | ArrayDeque ad = new ArrayDeque(); |
ArrayDeque(Collection c) | This constructor is used to create an ArrayDeque containing all the elements the same as that of the specified collection. | ArrayDeque ad = new ArrayDeque(Collection c); |
ArrayDeque(int numofElements) | This constructor is used to create an empty ArrayDeque and holds the capacity to contain a specified number of elements. | ArrayDeque ad = new ArrayDeque(int numofElements) |
The ArrayDeque class provides implementations for all the methods present in Queue and Deque interface.
Method Name | Description |
---|---|
add() | inserts the specified element at the end of the array deque. |
addFirst() | inserts the specified element at the beginning of the array deque. |
addLast() | inserts the specified at the end of the array deque (equivalent to add()). |
offer() | inserts the specified element at the end of the array deque. |
offerFirst() | inserts the specified element at the beginning of the array deque. |
offerLast() | inserts the specified element at the end of the array deque. |
getFirst() | returns the first element of the array deque. |
getLast() | returns the last element of the array deque. |
peek() | returns the first element of the array deque. |
peekFirst() | returns the first element of the array deque (equivalent to peek()). |
peekLast() | returns the last element of the array deque. |
remove() | returns and removes an element from the first element of the array deque. |
remove(element) | returns and removes the specified element from the head of the array deque. |
removeFirst() | returns and removes the first element from the array deque (equivalent to remove()). |
removeLast() | returns and removes the last element from the array deque. |
poll() | returns and removes the first element of the array deque. |
pollFirst() | returns and removes the first element of the array deque (equivalent to poll()). |
pollLast() | returns and removes the last element of the array deque. |
clear() | To remove all the elements from the array deque, we use the clear() method. |
element() | Returns an element from the head of the array deque. |
contains(element) | Searches the array deque for the specified element. If the element is found, it returns true, if not it returns false. |
size() | Returns the length of the array deque. |
toArray() | Converts array deque to array and returns it. |
clone() | Creates a copy of the array deque and returns it. |
The ArrayDeque class provides various methods to perform different operations on ArrayDeque. We will look at some commonly used ArrayDeque operations.
import java.util.ArrayDeque;
class Main {
public static void main(String[] args) {
ArrayDeque<String> animals= new ArrayDeque<>();
// Using add()
animals.add("Dog");
// Using addFirst()
animals.addFirst("Cat");
// Using addLast()
animals.addLast("Horse");
System.out.println("ArrayDeque: " + animals);
}
}
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);
}
}
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("First Element: " + firstElement);
// Get the last element
String lastElement = animals.getLast();
System.out.println("Last Element: " + lastElement);
}
}
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("Head Element: " + element);
// Using peekFirst()
String firstElement = animals.peekFirst();
System.out.println("First Element: " + firstElement);
// Using peekLast
String lastElement = animals.peekLast();
System.out.println("Last Element: " + lastElement);
}
}
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);
// Using remove()
String element = animals.remove();
System.out.println("Removed Element: " + element);
System.out.println("New ArrayDeque: " + animals);
// Using removeFirst()
String firstElement = animals.removeFirst();
System.out.println("Removed First Element: " + firstElement);
// Using removeLast()
String lastElement = animals.removeLast();
System.out.println("Removed Last Element: " + lastElement);
}
}
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);
// Using poll()
String element = animals.poll();
System.out.println("Removed Element: " + element);
System.out.println("New ArrayDeque: " + animals);
// Using pollFirst()
String firstElement = animals.pollFirst();
System.out.println("Removed First Element: " + firstElement);
// Using pollLast()
String lastElement = animals.pollLast();
System.out.println("Removed Last Element: " + lastElement);
}
}
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("\nArrayDeque in reverse order: ");
// Using descendingIterator()
Iterator<String> desIterate = animals.descendingIterator();
while(desIterate.hasNext()) {
System.out.print(desIterate.next());
System.out.print(", ");
}
// Iterate an elements using for each loop
for(String s: animals) {
System.out.println(s);
}
}
}