Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Priority Queue Class In Java

Last Updated on December 14, 2022 by Prepbytes

Queue:

The queue is a linear data structure that works on the principle of First in First out (FIFO). In the queue, the element which is added at least recently is removed first from it. For example, a queue of consumers for a resource or service where the consumer who comes first will be served first.

Priority Queue:

Priority queue is an abstract data type, It is a type of queue in which each element has a priority assigned to it. The priority of the element determines the order in which elements are removed from the priority queue. In the priority queue, all the elements are arranged either in ascending order or descending order.

Priority Queue Properties:

• In the priority queue, every element has priority assigned to it.
• The element with highest priority first.
• If two elements are having the same priority then they are served according to their order in the queue.

Key Points of priority queue:

• Priority queue doesn’t allow null.
• Priority queues are unbound queues.
• We can not create a priority queue of objects which are non-comparable.
• Priority queue provides O(log N) time for add(insertion) and poll(removal) methods.
• Priority queue inherits methods from abstractQueue, abstractCollection, collection and object class.

Constructors:

• PriorityQueue(): It creates a priority queue with default capacity that orders its elements according to their order.

PriorityQueue PQ = new PriorityQueue<>();

• PriorityQueue(Collection \ C): It creates a priority queue containing all the elements in a specified collection.

PriorityQueue PQ = new PriorityQueue<>(CollectionC);

• PriorityQueue(int initialCapacity): It creates a priority queue with some initial specified capacity which orders the elements according to their natural order.

PriorityQueue PQ = new PriorityQueue<>(int initialCapacity);

• PriorityQueue(int initialCapacity, Comparator \ comparator): It creates a priority queue with the specified initial capacity in which elements are ordered in accordance to their specified comparator

PriorityQueue PQ = new PriorityQueue<>(int initialCapacity,Comparator<> comparator);

• PriorityQueue(PriorityQueue \PQ): It creates a priority queue containing the elements in the specified priority queue.

PriorityQueue PQ = new PriorityQueue(PriorityQueue PQ).

• PriorityQueue(SortedSet \ PQ): It Creates a priority queue containing all the elements in the sorted set.

priorityQueuePQ = new priorityQueue(SortedSet PQ).

Operations of Priority Queue:

• Adding element: To insert an element into the priority queue we’ll use add() method. In the priority queue, all the elements are stored based on their priority i.e. in ascending order by default.
Code:

```import java.util.*;
import java.io.*;

public class PriorityQueueDemo {

public static void main(String args[])
{
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i=0;i<3;i++){
}
System.out.println(pq);
}
}

```

• Removing elements: To remove an element from the priority queue, we’ll use remove() method. If there are multiple objects which will be removed then the element which occurs first will be removed first. ALso we can use the poll() method to remove and return it.
Code:

```import java.util.*;
import java.io.*;

public class PriorityQueueDemo {

public static void main(String args[])
{
PriorityQueue<String> pq = new PriorityQueue<>();

System.out.println("Initial PriorityQueue " + pq);

pq.remove("prep");

System.out.println("After Remove - " + pq);

System.out.println("Poll Method - " + pq.poll());

System.out.println("Final PriorityQueue - " + pq);
}
}
```

• Accessing the elements: As we know, Queue follows FIFO(First In First Out)principle, therefore we can access only the head of the queue. To access the element we’ll use the peek() method.
Code:

```import java.util.*;

class PriorityQueueDemo {

public static void main(String[] args)
{

PriorityQueue<String> pq = new PriorityQueue<>();
System.out.println("PriorityQueue: " + pq);

String element = pq.peek();
System.out.println("Accessed Element: " + element);
}
}
```

• Iterating the priority queue: There are multiple ways to traverse the priority queue, but the most efficient and famous way is to convert the queue into an array and then traverse it using a for loop. Also, queues have an inbuilt iterator for iterating the queue.
Code:

```

import java.util.*;

class PriorityQueueDemo {

public static void main(String args[])
{
PriorityQueue<String> pq = new PriorityQueue<>();