Queue vs Deque

In this post, we will learn the difference between Queue and Deque in Java. This is a frequently asked
question in Java interviews for beginners. Let's dive into it.

Difference between Queue and Deque in Java

Queue Deque
A Queue is a data structure that follows the FIFO (First In First Out) principle. A Deque (Double Ended Queue) is a data structure that allows the insertion and removal of elements from both ends (front and rear).
Elements in a Queue are added at the rear and removed from the front. In a Deque, elements can be added or removed from both the front and the rear.
Standard operations in Queue include enqueue (add), dequeue (remove), and peek (get the top element). Standard operations in Deque include addFirst, addLast, removeFirst, removeLast, peekFirst, and peekLast.
Queue can be implemented as an array or a LinkedList. Deque can be implemented using a doubly linked list or a circular array.
A Queue is used when we need to maintain the order of elements. A Deque is used when we need to check elements or add/remove elements from both ends like in a palindrome checker.
The Queue doesn't support all List operations. Deque supports all List operations.
Example implementation in Java: LinkedList, PriorityQueue. Example implementation in Java: ArrayDeque, LinkedList.

References


Comments