Java SE API
PriorityQueue
Queue that serves the least element under its ordering. Iteration is not guaranteed sorted; tied heads are arbitrary unless the ordering distinguishes them.
Formal Syntax & Structure
java syntaxjava
class PriorityQueue<E>Working Example
Example Snippetjava
import java.util.*;public class Main { record Job(int priority, int sequence, String name) implements Comparable<Job> { public int compareTo(Job other) { int p = Integer.compare(priority, other.priority); if (p != 0) return p; int s = Integer.compare(sequence, other.sequence); return s != 0 ? s : name.compareTo(other.name); } } public static void main(String[] args) { Queue<Job> jobs = new PriorityQueue<>(); jobs.offer(new Job(2, 1, "Report")); jobs.offer(new Job(1, 2, "Alert")); jobs.offer(new Job(1, 3, "Backup")); while (!jobs.isEmpty()) System.out.println(jobs.poll().name()); }}