Skip to main content

Priority Queue Example in Java

3 min read Updated May 29, 2026
Share:
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

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. import java.util.Comparator; imports a class used later in the program.

  3. import java.util.PriorityQueue; imports a class used later in the program.

  4. int[] array = { 1, 10, 5, 3, 4, 7, 6, 9, 8 }; updates a variable used in the calculation or output.

  5. PriorityQueue pq1 = new PriorityQueue(); updates a variable used in the calculation or output.

  6. A println / print call writes text to the console — part of the sample output below.

  7. PQsort pqs = new PQsort(); updates a variable used in the calculation or output.

  8. A println / print call 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 .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Priority Queue program demonstrate?
It shows how to implement priority queue in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials