Data Structures & Algorithms Notes Page
import java.util.PriorityQueue;
public class PriorityQueueExample
{
public static void main(String[] args)
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(30);
pq.add(10);
pq.add(20);
System.out.println(pq.poll()); // Output: 10 (smallest)
System.out.println(pq.peek()); // Output: 20 (next smallest)
}
}
add(value) or offer(value) → Adds a valuepoll() → Removes and returns the top priority elementpeek() → Returns (but doesn’t return) the top element