- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Deque in Java
The dequeue is a double ended queue and data elements can be added or removed from either end. The dequeue in Java is implemented using the java.util.Deque interface which is a subtype of the java.util.Queue interface.
A program that demonstrates some of the methods of a dequeue is given as follows −
Example
import java.util.*; public class Example { public static void main(String[] args) { Deque<String> d = new LinkedList<String>(); d.add("5"); d.addFirst("1"); d.addLast("9"); d.push("7"); d.offer("8"); d.offerFirst("6"); d.offerLast("2"); System.out.println("The deque is: " + d); System.out.print("
Dequeue using standard Iterator: "); Iterator i = d.iterator(); while (i.hasNext()) { System.out.print(i.next() + " "); } System.out.println("
Using peek, the element at head of the deque is: " + d.peek()); System.out.println("The deque after peek: " + d); System.out.println("
Using pop, the element removed from the head of the deque is: " + d.pop()); System.out.println("The deque after pop: " + d); System.out.println("
Does the deque contain element 8: " + d.contains("8")); d.removeFirst(); d.removeLast(); System.out.println("
Deque after removing the first and last elements is: " + d); } }
Output
The deque is: [6, 7, 1, 5, 9, 8, 2] Dequeue using standard Iterator: 6 7 1 5 9 8 2 Using peek, the element at head of the deque is: 6 The deque after peek: [6, 7, 1, 5, 9, 8, 2] Using pop, the element removed from the head of the deque is: 6 The deque after pop: [7, 1, 5, 9, 8, 2] Does the deque contain element 8: true Deque after removing the first and last elements is: [1, 5, 9, 8]
Now let us understand the above program.
Various functions are used to perform operations on a deque. The add(), offer(), offerLast() as well as the addLast() function adds an element to the deque tail. The addFirst(), offerFirst() as well as the push() function adds an element to the deque head. The code snippet that demonstrates this is given as follows.
d.add("5"); d.addFirst("1"); d.addLast("9"); d.push("7"); d.offer("8"); d.offerFirst("6"); d.offerLast("2");
Then the deque is printed. After that it is printed using a standard iterator. The code snippet that demonstrates this is given as follows.
System.out.println("The deque is: " + d); System.out.print("
Dequeue using standard Iterator: "); Iterator i = d.iterator(); while (i.hasNext()) { System.out.print(i.next() + " "); }
Then peek() is used to see the element at the head of the deque and pop() is used to remove the element at the head of the deque. The code snippet that demonstrates this is given as follows −
System.out.println("
Using peek, the element at head of the deque is: " + d.peek()); System.out.println("The deque after peek: " + d); System.out.println("
Using pop, the element removed from the head of the deque is: " + d.pop()); System.out.println("The deque after pop: " + d);
The contains() function is used to check if an element is contained in the deque. The removeFirst() and removeLast() functions remove the first and last elements of the deque respectively. The code snippet that demonstrates this is given as follows −
System.out.println("
Does the deque contain element 8: " + d.contains("8")); d.removeFirst(); d.removeLast(); System.out.println("
Deque after removing the first and last elements is: " + d);
- Related Articles
- Deque interface in Java
- deque::at() and deque::swap() in C++ STL
- deque::begin() and deque::end in C++ STL
- deque::empty() and deque::size() in C++ STL
- deque::operator= and deque::operator[] in C++ STL
- deque front( ) and deque back( ) in C++ in STL
- Deque emplace_front( ) and deque emplace_back( ) in C++ in STL
- deque::at() and deque::swap() in C++ programming STL
- Deque in Python
- DEQUE CRBEGIN() in C++
- DEQUE CBEGIN() in C++
- Python - Deque
- A Deque Class in C#
- deque::push_front() in C++ STL
- Deque shrink_to_fit in C++ STL
