Java Intermediate

0% completed

Previous
Next
Basics of Queue

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle. In Java, the Queue interface, which is part of the java.util package, defines a collection designed for holding elements prior to processing. Queues are commonly used for task scheduling, managing print jobs, and other similar scenarios where the order of processing is important.

Queues offer operations for inserting elements at the rear and removing elements from the front. There are various implementations of the Queue interface, such as LinkedList, PriorityQueue, and ArrayDeque, each with its own specific behaviors.

Syntax for Using the Queue Interface

Queue<Type> queue = new LinkedList<Type>();
  • Explanation:
    • Queue<Type>: Declares a queue that holds elements of the specified Type.
    • new LinkedList<Type>(): Creates an instance of a queue using the LinkedList class, which implements the Queue interface.

Queue Interface Hierarchy

The Queue interface is a subinterface of the Collection interface. This means that every Queue inherits the basic collection operations such as adding, removing, and iterating over elements. The hierarchy looks like this:

  • Collection (root interface for collections)
    • Queue (defines additional queue-specific methods)
Image

Some implementations also provide more specialized behaviors:

  • Deque (double-ended queue): Allows insertion and removal at both ends.
  • PriorityQueue: Orders its elements according to their natural ordering or a custom Comparator.

This hierarchical structure means that you can use Queue methods along with all the methods inherited from Collection, making queues very versatile.

Common Methods in the Queue Interface

The table below summarizes the common methods provided by the Queue interface along with their descriptions:

MethodDescription
add(E e)Inserts the specified element into the queue; throws an exception if insertion fails.
offer(E e)Inserts the specified element into the queue; returns false if insertion fails.
remove()Retrieves and removes the head of the queue; throws an exception if the queue is empty.
poll()Retrieves and removes the head of the queue; returns null if the queue is empty.
element()Retrieves, but does not remove, the head of the queue; throws an exception if the queue is empty.
peek()Retrieves, but does not remove, the head of the queue; returns null if the queue is empty.

Examples

Example 1: Basic Queue Operations Using LinkedList

Below is a simple example that demonstrates the basic operations of the Queue interface using the LinkedList implementation. In this example, we will:

Java
Java

. . . .

Example Explanation:

  • Queue Creation:
    • A Queue<String> named taskQueue is created using the LinkedList implementation.
  • Inserting Elements:
    • The offer() method is used to insert "Task 1", "Task 2", and "Task 3" into the queue.
  • Inspecting the Head:
    • The peek() method retrieves the head element ("Task 1") without removing it.
  • Removing Elements:
    • The poll() method removes and returns the head element ("Task 1") from the queue.
  • Outcome:
    • The output displays the initial queue, the head element, the removed element, and the updated queue after removal, demonstrating FIFO behavior.

Example 2: Basic Queue Operations Using a For-Each Loop

This example demonstrates how to create a queue using the LinkedList implementation, insert elements, and iterate over the queue using a for-each loop.

Java
Java

. . . .

Example Explanation:

  • Queue Creation:
    • A Queue<String> is created using the LinkedList implementation.
  • Inserting Elements:
    • Elements are added using the offer() method.
  • Iteration:
    • The for-each loop automatically traverses all elements in the queue.
  • Outcome:
    • Each task in the queue is printed in FIFO order.

The Queue interface in Java is designed for managing elements in a FIFO (First-In-First-Out) order. It extends the Collection interface, meaning it inherits many common operations while also providing specialized queue methods such as offer(), poll(), and peek(). You can iterate over a queue using either a for-each loop or an Iterator, giving you flexibility in how you process the elements. Understanding these basic operations is essential for using queues in real-world applications like task scheduling and event handling.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next