Priority Queue Example in Java
On this page (10sections)
Introduction
Priority Queue is a classic Java console program that demonstrates the concept with complete source code and sample output. Classic data structures such as stack, queue and linked list implemented in Java.
This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.
Definition
A priority queue is an abstract data type which is like a regular queue or stack data structure, but where additionally each element has a “priority” associated with it. In a priority queue, an element with high priority is served before an element with low priority. If two elements have the same priority, they are served according to their order in the queue. While priority queues are often implemented with heaps, they are conceptually distinct from heaps.
Syntax
PriorityQueue Variable_name = new PriorityQueue();
Priority Queue Example Program
import java.util.Comparator;
import java.util.PriorityQueue;
public class PriorityQueueDemo {
static class PQsort implements Comparator {
public int compare(Integer one, Integer two) {
return two - one;
}
}
public static void main(String[] args) {
int[] array = { 1, 10, 5, 3, 4, 7, 6, 9, 8 };
PriorityQueue pq1 = new PriorityQueue();
for (int i : array) {
pq1.offer(i);
}
System.out.println("PriorityQueue1: " + pq1);
PQsort pqs = new PQsort();
PriorityQueue pq2 = new PriorityQueue(10, pqs);
for (int i : array) {
pq2.offer(i);
}
System.out.println("PriorityQueue2: " + pq2);
System.out.println("size: " + pq2.size());
System.out.println("peek: " + pq2.peek());
System.out.println("size: " + pq2.size());
System.out.println("poll: " + pq2.poll());
System.out.println("size: " + pq2.size());
System.out.print("PriorityQueue2: " + pq2);
}
}
Sample Output
PriorityQueue1: [1, 3, 5, 8, 4, 7, 6, 10, 9]
PriorityQueue2: [10, 9, 7, 8, 3, 5, 6, 1, 4]
size: 9
peek: 10
size: 9
poll: 10
size: 8
PriorityQueue2: [9, 8, 7, 4, 3, 5, 6, 1]
When to use
Use this priority queue example when learning or revising core Java syntax.
How it works
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
import java.util.Comparator;imports a class used later in the program. -
import java.util.PriorityQueue;imports a class used later in the program. -
int[] array = { 1, 10, 5, 3, 4, 7, 6, 9, 8 };updates a variable used in the calculation or output. -
PriorityQueue pq1 = new PriorityQueue();updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
PQsort pqs = new PQsort();updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below.
Best Practices
- Use meaningful variable and class names that describe their purpose.
- Compile and run the program locally — modify values to see how output changes.
- Read compiler errors carefully; they usually point to the exact line to fix.
Common Mistakes
- Copying code without understanding each line — practice by changing one statement at a time.
- Mismatching the public class name and the
.javafilename. - Forgetting semicolons at the end of statements.