Updated March 24, 2023
Overview of PriorityQueue in Java
Normally, objects in Queue are placed in the FIFO order. i.e., First-in-First-Out. In certain cases, objects have to be processed based on their priority, and at that time, Java PriorityQueue came into action. In addition to that, PriorityQueue has certain features they are:
- It is an unbounded queue.
- In default, the ordering of elements is in a natural order.
- Null values are not allowed.
- Non-comparable objects can’t be used for creating a PriorityQueue.
Declaration of Java PriorityQueue
Java PriorityQueue can be declared using the below syntax.
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
Constructors of Java PriorityQueue
Following are the constructors that are commonly used in Java PriorityQueue:
1. PriorityQueue(): A PriorityQueue will be created with 11 as its initial capacity is in default. Moreover, the elements are ordered based on natural ordering.
2. PriorityQueue(Collection<? extends E> c): A PriorityQueue will be created with elements in the mentioned collection.
3. PriorityQueue(int ic): A PriorityQueue will be created with the initial capacity ic mentioned. Moreover, the elements are ordered based on natural ordering.
4. PriorityQueue(int ic, Comparator<? super E> comparator): A PriorityQueue will be created with the initial capacity ic mentioned. Moreover, the elements are ordered based on the mentioned comparator.
5. PriorityQueue(PriorityQueue<? extends E> c): A PriorityQueue will be created with elements in the mentioned PriorityQueue.
6. PriorityQueue(SortedSet<? extends E> c): A PriorityQueue will be created with elements in the mentioned sorted set.
Methods
Now, let us see some of the commonly used methods in Java PriorityQueue:
1. add(E e): Element e will be added to the PriorityQueue on calling this method.
2. size(): Count of elements in the collection will be returned.
3. clear(): All the elements in the PriorityQueue will be removed.
4. comparator(): Comparator that is used to sort the queue will be returned. If natural ordering is used, null will be returned.
5. contains(Objecto): If the queue contains the mentioned element o, true will be returned.
6. iterator(): The iterator that is used over the elements in the queue will be returned.
7. offer(Ee): Mentioned element e will be inserted in the queue.
8. peek(): Head of the PriorityQueue will be retrieved, not removed. In case no elements are present, null will be returned.
9. poll(): Head of the PriorityQueue will be retrieved and removed. In case no elements are present, null will be returned.
10. remove(Objecto): A single instance of the mentioned element will be removed from the queue.
11. toArray(): An array of all the elements in the queue will be returned.
12. toArray(T[] a): An array of all the elements in the queue will be returned in which the runtime will be as that of the mentioned array.
Examples of Java PriorityQueue
Given below are the examples of Java PriorityQueue:
Example #1
Sample program to create a PriorityQueue.
Code:
import java.util.Iterator;
import java.util.PriorityQueue;
class Main{
public static void main(String args[]){
//create a PriorityQueue
PriorityQueue<String> q=new PriorityQueue<String>();
//add elements to the queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
System.out.println("Head of the queue:"+q.element());
System.out.println("Head of the queue :"+q.peek());
//Retrieve elements in queue using iterator
System.out.println("Queue elements are :");
Iterator it=q.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
//remove the element from queue
q.remove();
//remove the head of the queue
q.poll();
System.out.println("\n Queue after the removal of 2 elements :");
//Retrieve elements in queue using iterator
Iterator<String> it2=q.iterator();
while(it2.hasNext())
{
System.out.println(it2.next());
}
}
}
Output:
Working of the sample program:
- Create a PriorityQueue q.
- Add elements to the Queue.
- Print the head of the queue.
- Print all the elements in the Queue using iterator.
- Remove two elements.
- Print all the elements in the Queue after the removal of two elements.
Example #2
Sample program to create a PriorityQueue using a comparator.
Code:
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
class PriorityQueueExample{
public static void main(String[] args) {
//Create a custom comparator. In this, length of 2 strings are getting compared
Comparator<String> cmp = new Comparator<String>()
{
@Override
public int compare(String str1, String str2) {
return str1.length() - str2.length();
}
};
// PriorityQueue creation with Comparator
PriorityQueue<String> q = new PriorityQueue<>(cmp);
// Add elements to the Queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
q.add("Sam");
q.add("Elsa");
q.add("Kukku");
q.add("Mathu");
q.add("Radha");
// Remove elements from the Queue
while (!q.isEmpty()) {
System.out.println(q.remove());
}
}
}
Output:
Working of the sample program:
- Create a custom comparator where the elements in the Queue are compared based on the length.
- Create a PriorityQueue q.
- Add elements to the Queue.
- Remove the elements from the queue based on the length of the strings.
- Print the elements in the queue in the order of removal.
Example #3
Sample program to implement a PriorityQueue by making use of different methods.
Code:
import java.util.*;
class Main{
public static void main(String args[]){
//create a PriorityQueue
PriorityQueue<String> q=new PriorityQueue<String>();
//add elements to the queue
q.add("Anna");
q.add("Annamu");
q.add("Adam");
q.add("Iza");
q.add("Thukidi");
System.out.println("Head of the queue:"+q.element());
System.out.println("Head of the queue :"+q.peek());
//Retrieve elements in queue using iterator
System.out.println("Queue elements are :");
Iterator it=q.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
//remove the element from queue
q.remove();
//remove the head of the queue
q.poll();
// Check whether the element Anna is present in queue using the method Contains()
boolean b = q.contains("Anna");
System.out.println("Is there any element Anna in the PriorityQueue ? " + b);
//Check whether the element Iza is present in queue using the method Contains()
boolean bl = q.contains("Iza");
System.out.println("Is there any element Anna in the PriorityQueue ? " + bl);
}
}
Output:
Working of the sample program:
- Create a PriorityQueue q.
- Add elements and Print the head of the queue.
- Print all the elements in the Queue using iterator.
- Remove two elements using the methods remove() and poll().
- Check whether the two elements Anna and Iza are available in Queue using the method Contains().
Recommended Articles
This is a guide to PriorityQueue in Java. Here we discuss the introduction, constructors, methods, and examples of java PriorityQueue. You may also have a look at the following articles to learn more –