Queue interface, Deque interface, ArrayDeque class & PriorityQueue class

Dont worry we study this hirarchy one by one, first you understand these points.

  • ArrayDeque implements Deque extends Queue.
  • LinkedList implements Deque extends Queue.
  • PriorityQueue implements Queue.

Now see the below image.

#

Queue interface

Queue:

  • The Queue interface represents a collection designed for holding elements prior to processing. It follows the FIFO (First-In-First-Out) principle.
  • It is commonly used in scenarios where elements need to be processed in the order they were added.

Methods in queue interface:

MethodsDescription
boolean add(E e)
  • Inserts the specified element into the queue.
  • Returns true if the element was added successfully.
  • Throws an IllegalStateException if the element cannot be added (due to capacity restrictions).
boolean offer(E e)
  • Inserts the specified element into the queue.
  • Returns true if the element was added successfully, false otherwise.
  • This method is preferable to add for capacity-restricted queues.
E remove()
  • Retrieves and removes the head of the queue.
  • Throws a NoSuchElementException if the queue is empty.
E poll()
  • Retrieves and removes the head of the queue, or returns null if the queue is empty.
E element()
  • Retrieves, but does not remove, the head of the queue.
  • Throws a NoSuchElementException if the queue is empty.
E peek()
  • Retrieves, but does not remove, the head of the queue, or returns null if the queue is empty.
Code Example

Deque Interface

  • Deque:

  • The Deque interface (Double-Ended Queue) extends the Queue interface and allows elements to be added or removed from both ends of the queue.
  • It is useful in scenarios where elements need to be processed from both ends, such as implementing a stack or a queue.

Methods:

OperationMethods
Adding Elements

void addFirst(E e)

  • Inserts the specified element at the front of the deque.
  • Throws a NullPointerException if the specified element is null.

void addLast(E e)

  • Inserts the specified element at the end of the deque.
  • Throws a NullPointerException if the specified element is null.

boolean offerFirst(E e)

  • Inserts the specified element at the front of the deque.
  •  Returns true if the element was added successfully, false otherwise.

boolean offerLast(E e)

  • Inserts the specified element at the end of the deque.
  • Returns true if the element was added successfully, false otherwise.
Removing Elements

E removeFirst()

  • Retrieves and removes the first element of the deque.
  • Throws a NoSuchElementException if the deque is empty.

E removeLast()

  • Retrieves and removes the last element of the deque.
  • Throws a NoSuchElementException if the deque is empty.

E pollFirst()

  • Retrieves and removes the first element of the deque, or returns null if the deque is empty.

E pollLast()

  • Retrieves and removes the last element of the deque, or returns null if the deque is empty.
Retrieving Elements

E getFirst()

  • Retrieves, but does not remove, the first element of the deque.
  • Throws a NoSuchElementException if the deque is empty.

E getLast()

  • Retrieves, but does not remove, the last element of the deque.
  • Throws a NoSuchElementException if the deque is empty.

E peekFirst()

  • Retrieves, but does not remove, the first element of the deque, or returns null if the deque is empty.

E peekLast()

  • Retrieves, but does not remove, the last element of the deque, or returns null if the deque is empty.
Stack Operations

void push(E e)

  • Pushes an element onto the stack represented by the deque (equivalent to addFirst).
  • Throws a NullPointerException if the specified element is null.

E pop()

  • Pops an element from the stack represented by the deque (equivalent to removeFirst).
  • Throws a NoSuchElementException if the deque is empty.
Code Example
Comparison of Queue and Deque methods
Queue Method		Equivalent Deque Method
-------------		-----------------------
add(e)				addLast(e)
offer(e)			offerLast(e)
remove()			removeFirst()
poll()				pollFirst()
element()			getFirst()
peek()				peekFirst()

ArrayDeque class

ArrayDeque:

  • ArrayDeque is a resizable-array implementation of the Deque interface. It is not thread-safe but provides fast and efficient implementations of the methods defined by the Deque interface.
  • Underlying Data Structure: Resizable array.
  • Inherits all methods from Deque and Queue.

Constructors:

  • ArrayDeque(): Constructs an empty deque.
  • ArrayDeque(Collection c): Constructs a deque containing the elements of the specified collection.
  • ArrayDeque(int intialCapacity): Constructs an empty deque with an initial capacity sufficient to hold the specified number of elements.

Key Points:

  • Resizable Array: ArrayDeque dynamically resizes its array to accommodate more elements.
  • No Capacity Restrictions: It does not have capacity restrictions, and it can grow as needed.
  • Fast Operations: ArrayDeque provides constant time for add, remove, and peek operations at both ends.
  • Not Thread-Safe: It is not synchronized, so it is not safe for concurrent access by multiple threads.
  • Preferred for Stack and Queue Operations: It is often used as a stack or a queue due to its efficiency.
Code Example