ArrayDeque in Java
In the previous blog, we learned about the Dequeue Interface. If you want to know more about it visit Dequeue Interface in Java. In this blog, we will go through another interface of Queue i.e. ArrayDeque.
ArrayDeque Interface
ArrayDeque stands for Array Double-Ended Queue. ArrayDeque grows and allows users to add or remove an element from both sides of the queue. ArrayDeque is present in java. util.ArrayDeque.
Syntax of ArrayDeque
Methods of ArrayDeque
- add()
- addFirst()
- addLast()
- offer()
- offerFirst()
- offerLast()
- peek()
- peekFirst()
- peekLast()
- getFirst()
- getLast()
- remove()
- removeFirst()
- poll()
- pollFirst()
- pollLast()
add() method adds the specified element at the end of the array deque.
addFirst() method adds the specified element at the start of the array deque.
addLast() method adds the specified at the end of the array deque (same as add()).
offer() method adds the specified element at the end of the array deque.
offerFirst() method adds the specified element at the start of the array deque.
offerLast() appends the specified element to the end of the array deque.
Example of ArrayDeque using the above three methods
Output
peek() method returns the first element of the array deque.
peekFirst() method returns the first element of the array deque (same as peek()).
peekLast() method returns the last element of the array deque.
The first element of the array deque is returned by the getFirst() method.getFirst() method returns the first element of the array deque.
getLast() method returns the last element of the array deque.
Example of ArrayDeque using the above methods
Output
remove() method returns and removes an element from the first element of the array deque
removeFirst() method returns and removes the first element from the array deque (same as remove())
removeLast() method returns and removes the last element from the array deque
poll() method returns and removes the first element of the array deque
pollFirst() method returns and removes the first element of the array deque (same as poll())
pollLast() method returns and removes the last element of the array deque
Example of ArrayDeque using the above mentioned methods
Output