xxxxxxxxxx
import java.util.PriorityQueue;
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
// Enqueue elements with priorities
priorityQueue.offer(5);
priorityQueue.offer(2);
priorityQueue.offer(8);
priorityQueue.offer(1);
// Dequeue elements based on priority
while (!priorityQueue.isEmpty()) {
System.out.println(priorityQueue.poll()); // Outputs: 1, 2, 5, 8
}
}
}