020-004-011

PriorityQueue with Custom Priority

Hard

Problem Description

PriorityQueue with Custom Priority

In this problem: You will configure a PriorityQueue with a custom Comparator to process tasks in priority order.

Learning Objective: Understand PriorityQueue with custom priority using Comparator

Overview

PriorityQueue retrieves elements in priority order. The default is natural order, but you can specify a Comparator for custom priority.

Specifications

  • Task class: has name(String) and priority(int, lower = higher priority) fields
  • Create PriorityQueue<Task> sorted by priority ascending (lower values first)
  • Add tasks: Task("Low", 3), Task("High", 1), Task("Medium", 2)
  • Use poll() to remove all tasks and output in "[{priority}] {name}" format

Output Format

[1] High
[2] Medium
[3] Low

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up