About Lesson
Deque Interface
In Java, the Deque interface is under java.util.Deque and it is a subtype of java.util.Queue interface. A Deque is a double-ended queue that means addition and deletion of the elements can be done from both the ends. The Deque can be used as a Queue or as Stack i.e First-in-first-out or Last-in-last-out.
Below is the hierarchy of Deque interface
Below are the methods of Deque interface
S.No. | Method | Description |
---|---|---|
1 | add(element) | It is used for adding elements at the tail of the Deque. |
2 | addFirst(element) | It is used for adding elements at the head of the Deque. |
3 | addLast(element) | It is used for adding elements at the tail of the Deque. |
4 | offer(element) | It is used to add an element at the tail and returns a Boolean value if added successfully. |
5 | offerFirst(element) | It is used to addan element at the head and returns a Boolean value if added successfully. |
6 | offerLast(element) | It is used to addan element at the tail and returns a Boolean value if added successfully. |
7 | iterator() | It is used to iterate the deque. |
8 | descendingIterator() | It is used to iterate for the reverse order in the deque. |
9 | push(element) | It is used for adding elements at the head of the deque. |
10 | pop(element) | It is used for removing elements from the head of the deque. |
11 | removeFirst() | It is used for removing elements from the head of the deque. |
12 | removeLast() | It is used for removing elements from the tail of the deque |
13 | poll() | It is used to get and remove the first element from the deque. If the deque is empty then it returns null. |
14 | pollFirst() | It is used to get and remove the first element from the deque. If the deque is empty then it returns null. |
15 | pollLast() | It is used to get and remove the last element from the deque. If the deque is empty then it returns null. |
16 | peek() | It is used for removing the head of the deque. |
17 | peekFirst() | It is used for removing the head of the deque. |
18 | peekLast() | It is used for removing the tail of the deque. |
Example:
Â