Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

util.PriorityQueue: practical reference

util.PriorityQueue: practical reference: PriorityQueue<E> is an unbounded heap queue whose head is the least element under natural ordering or the supplied Comparator.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

java.util.PriorityQueue

PriorityQueue is an unbounded heap queue whose head is the least element under natural ordering or the supplied Comparator.

Use it to select the next best-priority task without fully sorting all remaining elements.

Its iterator is not in priority order, the queue is not thread-safe, and mutating ordering state breaks the position of an inserted element.

It rejects null but permits duplicates; peek and poll return null when empty, and equal-priority elements have unspecified ordering.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var queue = new java.util.PriorityQueue<Integer>();
        queue.add(3);
        queue.add(1);
        queue.add(2);
        System.out.println(queue.remove());
    }
}

Result

1

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources