riorityQueue class which is implemented in the collection framework provides us a way to process the objects based on the priority. It is known that a queue follows First-In-First-Out algorithm, but sometimes the elements of the queue are needed to be processed according to the priority, that’s when the PriorityQueue comes into play. Let’s see how to create a queue object using this class.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[])
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(10);
pq.add(20);
pq.add(30);
pq.add(40);
pq.add(50);
System.out.println("Initial PriorityQueue:\n" + pq);
pq.remove(50);
System.out.println("After Remove:\n" + pq);
}
}
Comments
Leave a comment