# PriorityQueue in Java

Hi coders! In this tutorial, we are learning about the PriorityQueue in Java. Since PriorityQueue is a Queue which follows First-In-First- algorithm but the PriorityQueue comes into picture when the elements of the Queue are needed to be accessed according to the priority.

Some points related to PriorityQueue:

• Non-comparable Objects’ PriorityQueue can not be created.
• PriorityQueue is unbounded queues.
• PriorityQueue inherits methods from AbstractQueue, Collection and Object class.
• Null pointer is not allowed in PriorityQueue.

### Some of the methods of PriorityQueue:

poll(): This function retrieves and removes the head of the PriorityQueue.

remove(): This function removes the element from theĀ  PriorityQueue.

peek(): This function retrieves the head of the PriorityQueue.

These are some of the functions of PriorityQueue class.

### Code for the PriorityQueue in Java

```import java.util.*;

public class Codespeedy
{
public static void main(String args[])
{
// Creating PriorityQueue of Integer type
PriorityQueue<Integer> pQue = new PriorityQueue<Integer>();
/* inserting elements to the PriorityQueue*/

/* Print the most prior element*/
System.out.println("Head of the priority queue:"+ pQue.peek());

/* Print all elements*/
System.out.println("The elements of the queue are:");
Iterator itr = pQue.iterator();
while (itr.hasNext())
System.out.println(itr.next());

}
}
//Code is provided by Anubhav Srivastava```
```Output:

The elements of the queue are:
4
7
5
9```

PriorityQueue is widely used in Dijkstra’s algorithm and prim’s algorithm.

### Some of the constructors of PriorityQueue are:

• PriorityQueue(): This is the basic constructor of PriorityQueue without any argument.
• PriorityQueue(Collection<E> c)
• PriorityQueue(int initialCap): Construct a PriorityQueue with the specified initial capacity.

Hence, PriorityQueue is a type of queue .

Hope you like the solution.