PriorityQueue in java

The PriorityQueue class is a part of the Java Collection framework which extends AbstractQueue class and implements the Serializable interface. As we already know Queue works based on FIFO(First-In-First-Out model). But sometimes we want to process the objects based on priority and then PriorityQueue class comes in the role.

PriorityQueue in java

Here is the table content of the article will we will cover this topic.
1. What is PriorityQueue in java?
2. Important points about Priority Queue java?
3. How to create a priority queue in java?
4. Constructors of PriorityQueue class?
5. How to add the element in PriorityQueue?
6. Remove element from PriorityQueue?
7. Some other methods?

What is PriorityQueue in java?

A PriorityQueue used to process all the objects based on the sorted order. It is an unbounded queue and the elements of the priority queue are ordered by default in the natural order. We can provide a custom order by proving a Comparator during the creation of the PriorityQueue.
The insertion operation in PriorityQueue performs based on the priority of the object. By default, PriorityQueue determines the priority in the natural order, we can change the priority of objects to provide a comparator.

Important points about Priority Queue java

1. PriorityQueue is an unbounded queue that grows dynamically. The default initial capacity of PriorityQueue is 11. We can change the initial capacity during the creation of the PriorityQueue.

2. PriorityQueue does not allow NULL objects. If we try to add NULL in PriorityQueue, then it will throw NullPointerException. Because we can’t create a PriorityQueue with the non-comparable element.

3. By default, PriorityQueue maintains the natural order of objects. Objects added to PriorityQueue MUST be comparable.

4. We can maintain the custom order of elements by use of Comparator. Here we have to implement the Comparator interface.

5. The least element will be the head of the PriorityQueue based on natural ordering or comparator based ordering.

6. PriorityQueue is not thread-safe.

How to create a priority queue in java

We can a PriorityQueue by the use of different constructors. The PriorityQueue class has a various constructor and each has a different use. If we want to create a PriorityQueue and sort the object in the natural order then we can use the default constructor. If we want to sort element based on some criteria then we can use the Comparator. Let’s discuss the java priority queue example.

Constructors of PriorityQueue class

1. PriorityQueue(): It is a default constructor of the PriorityQueue class. It is used to create an empty PriorityQueue with initial default capacity 11. It provides the natural order to all elements.

import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>(); 
	numberQueue.add(1);
	numberQueue.add(7);
	numberQueue.add(2);
	numberQueue.add(8);
	
	System.out.println("Elements from PriorityQueue: "+ numberQueue);
  }
}

Output: Elements from PriorityQueue: [1, 2, 7, 8]

2. PriorityQueue(Collection<E> c): ): It is used to create a PriorityQueue using the elements of the specified collection c.

import java.util.ArrayList;
import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	ArrayList<Integer> numbers = new ArrayList<Integer>();
	numbers.add(1);
	numbers.add(7);
	numbers.add(2);
	numbers.add(8);
	
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>(numbers); 
	System.out.println("Elements from PriorityQueue: "+ numberQueue);
  }
}

Output: Elements from PriorityQueue: [1, 2, 7, 8]

3. PriorityQueue(int initialCapacity): It is used to create an empty PriorityQueue with specified capacity. It provides the natural order to all elements.

import java.util.ArrayList;
import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>(10); 
	numberQueue.add(1);
	numberQueue.add(7);
	numberQueue.add(2);
	numberQueue.add(8);
	
	System.out.println("Elements from PriorityQueue: "+ numberQueue);
  }
}

Output: Elements from PriorityQueue: [1, 2, 7, 8]

4. PriorityQueue(int initialCapacity, Comparator<E> comparator): It is used to create an empty PriorityQueue with a specified initial capacity that orders its elements according to the specified comparator.

import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;
class Student
{  
    int rollNo;  
    String name;  
    int age;  
    Student(int rollno,String name,int age)
    {  
        this.rollNo=rollno;  
        this.name=name;  
        this.age=age;  
    }  
}
class RollNoComparator implements Comparator<Student>
{  
    @Override
    public int compare(Student student1, Student student2) 
    {
        if(student1.rollNo==student2.rollNo)  
        return 0;  
        else if(student1.rollNo>student2.rollNo)  
        return 1;  
        else  
        return -1;  
    }  
}  
public class ExampleOfPriorityQueue
{  
    public static void main(String args[])
    {  
        PriorityQueue<Student> queueOfStudent = new PriorityQueue<Student>(10, new RollNoComparator());  
        queueOfStudent.add(new Student(3,"Ram",26));  
        queueOfStudent.add(new Student(2,"Sham",27));  
        queueOfStudent.add(new Student(1,"Krishna",20));  
        
        System.out.println("Elements from queueOfStudent: ");
        for(Student stu : queueOfStudent)
        	System.out.println("RollNo: "+ stu.rollNo);
    }  
}  

Output: Elements from queueOfStudent:
RollNo: 1
RollNo: 3
RollNo: 2

5. PriorityQueue(PriorityQueue<E> c): It is used to create PriorityQueue with elements in the specified priority queue.

import java.util.ArrayList;
import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>(10); 
	numberQueue.add(10);
	numberQueue.add(1);
	numberQueue.add(7);
	numberQueue.add(2);
	numberQueue.add(8);
	
	PriorityQueue<Integer> newNumber = new PriorityQueue<Integer>(numberQueue);
	System.out.println("Elements from PriorityQueue: "+ newNumber);
  }
 }

Output: Elements from PriorityQueue: [1, 2, 7, 10, 8]

6. PriorityQueue(SortedSet<E> c): It is used to create PriorityQueue with elements in the specified sorted set.

import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.TreeSet; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	SortedSet<Integer> sortedSet = new TreeSet<Integer>(); 
	sortedSet.add(10);	
	sortedSet.add(1);
	sortedSet.add(7);
	sortedSet.add(2);
	sortedSet.add(8);
	
	PriorityQueue<Integer> newNumber = new PriorityQueue<Integer>(sortedSet);
	System.out.println("Elements from PriorityQueue: "+ newNumber);
  }
}

Output: Elements from PriorityQueue: [1, 2, 7, 8, 10]

How to add the element in PriorityQueue

1. add(E e): This method is used to add the given element in the PriorityQueue. Its return type is boolean. It throws NullPointerException if the specified element is null.

import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>();
	numberQueue.add(1);
	numberQueue.add(3);
	numberQueue.add(2);
	numberQueue.add(8);
	
	System.out.println("Elements from PriorityQueue: "+ numberQueue);
  }
}

Output: Elements from PriorityQueue: [1, 3, 2, 8]

Remove element from PriorityQueue

remove(Object o): This method is used to remove the first occurrence of the specified element from PriorityQueue. Its return type is boolean.

import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>();
	numberQueue.add(1);
	numberQueue.add(3);
	numberQueue.add(2);
	numberQueue.add(8);
	
	System.out.println("Before remove elements from PriorityQueue: "+ numberQueue);
	numberQueue.remove(3);
	System.out.println("Before remove elements from PriorityQueue: "+ numberQueue);
  }
}

Output: Before remove elements from PriorityQueue: [1, 3, 2, 8]
Before remove elements from PriorityQueue: [2, 3, 8]

poll(): This method is used to removes and return the first element from PriorityQueue. Its return type is E.

import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>();
	numberQueue.add(1);
	numberQueue.add(3);
	numberQueue.add(2);
	numberQueue.add(8);
	
	System.out.println("Before poll method PriorityQueue: "+ numberQueue);
	numberQueue.poll();
	System.out.println("After poll method PriorityQueue: "+ numberQueue);
  }
}

Output: Before poll method PriorityQueue: [1, 3, 2, 8]
After poll method PriorityQueue: [2, 3, 8]

Some other methods

peek(): This method is used to get to the first element from the PriorityQueue. Its return type is E.

import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>();
	numberQueue.add(1);
	numberQueue.add(3);
	numberQueue.add(2);
	numberQueue.add(8);
	
	System.out.println("peek method PriorityQueue: "+ numberQueue.peek());
  }
  
}

Output: peek method PriorityQueue: 1

offer(E e): This method is used to add the given element at the end of the PriorityQueue. Its return type is boolean.

import java.util.PriorityQueue; 
  
public class ExampleOfQueue 
{ 
  public static void main(String[] args) 
  { 
	PriorityQueue<Integer> numberQueue = new PriorityQueue<Integer>();
	numberQueue.add(1);
	numberQueue.add(3);
	numberQueue.add(2);
	numberQueue.add(8);
	
	System.out.println("offer method PriorityQueue: "+ numberQueue.offer(2));
  }
  
}

Output: offer method PriorityQueue: true

Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.