Cover Image for Java Queue & PriorityQueue
88 views

Java Queue & PriorityQueue

The Java Queue is an interface that represents a collection of elements with two primary operations: adding elements to the back (enqueue) and removing elements from the front (dequeue). It follows a First-In-First-Out (FIFO) order, meaning that the element added first will be the first to be removed. The Queue interface is part of the java.util package and is extended by several implementing classes, including PriorityQueue. Here, we’ll discuss Queue and its implementing class PriorityQueue:

Queue Interface:

  1. Common Methods:
  • add(E element): Adds the specified element to the queue if space is available; otherwise, it throws an exception.
  • offer(E element): Adds the specified element to the queue if space is available; returns true if successful, or false if the queue is full.
  • remove(): Removes and returns the element at the front of the queue; throws an exception if the queue is empty.
  • poll(): Removes and returns the element at the front of the queue; returns null if the queue is empty.
  • element(): Retrieves, but does not remove, the element at the front of the queue; throws an exception if the queue is empty.
  • peek(): Retrieves, but does not remove, the element at the front of the queue; returns null if the queue is empty.
  • size(): Returns the number of elements in the queue.
  • isEmpty(): Checks if the queue is empty.
  1. Implementing Classes:
  • Implementations of the Queue interface include LinkedList and PriorityQueue. LinkedList provides a basic FIFO queue, while PriorityQueue allows you to define a custom priority order for elements.

PriorityQueue:

  1. Ordering:
  • PriorityQueue is an implementation of the Queue interface that provides a priority queue behavior. Elements in a PriorityQueue are ordered based on their natural order (if they are Comparable) or a specified Comparator. The highest-priority element is at the front.
  1. Common Methods:
  • add(E element) (also known as offer(E element)): Adds the specified element to the queue according to its priority.
  • remove(): Removes and returns the highest-priority element.
  • poll(): Removes and returns the highest-priority element; returns null if the queue is empty.
  • peek(): Retrieves, but does not remove, the highest-priority element; returns null if the queue is empty.
  1. Custom Priority Order:
  • You can use a custom Comparator to define a specific order of elements in the PriorityQueue. This allows you to prioritize elements based on your own criteria.

Here’s an example of using PriorityQueue to create a priority queue of tasks with custom priorities:

import java.util.PriorityQueue;
import java.util.Comparator;

class Task {
    String name;
    int priority;

    public Task(String name, int priority) {
        this.name = name;
        this.priority = priority;
    }
}

public class PriorityQueueExample {
    public static void main(String[] args) {
        PriorityQueue<Task> taskQueue = new PriorityQueue<>(Comparator.comparingInt(task -> task.priority));

        taskQueue.offer(new Task("Task A", 3));
        taskQueue.offer(new Task("Task B", 1));
        taskQueue.offer(new Task("Task C", 2));

        while (!taskQueue.isEmpty()) {
            Task task = taskQueue.poll();
            System.out.println("Processing: " + task.name);
        }
    }
}

The tasks are added to a PriorityQueue based on their priority values. The tasks are processed in ascending order of priority, as defined by the custom Comparator. This demonstrates the use of PriorityQueue for managing tasks with custom priorities.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS