Last Updated : 23 Jul, 2025
In this article, we will demonstrate how to check if a Priority Queue is empty or not. The java.util.PriorityQueue.isEmpty() method is used to check if the Priority Queue is empty or not. Based on the Boolean value it returns the Response for the same.
Syntax:
Priority_Queue.isEmpty()
Parameters: There is no need to pass any parameter inside the function.
Return Value: The method returns a boolean value, ie. True if Priority Queue is empty else false.
Program to Check if a PriorityQueue is EmptyThe below programs illustrate the Java.util.PriorityQueue.isEmpty() method:
Example 1:The program showing that the PriorityQueue is not Empty:
Java
// Java code to illustrate isEmpty() method
// When Priority Queue contains some elements
import java.util.PriorityQueue;
// Driver Class
public class Main {
// Main Function
public static void main(String[] args)
{
// Creating a PriorityQueue
PriorityQueue PQ = new PriorityQueue<Integer>();
// Inserting some elements
PQ.add(6);
PQ.add(4);
PQ.add(55);
PQ.add(1);
// Checking whether PQ is empty or not
// by calling isEmpty() method on PQ
System.out.println(PQ.isEmpty());
}
}
Example 2:
The program showing that the PriorityQueue is Empty:
Java
// Java code to illustrate isEmpty() method
// When Priority Queue doesn't contains element
import java.util.PriorityQueue;
// Driver Class
public class Main {
// Main Function
public static void main(String[] args)
{
// Creating an empty PriorityQueue
PriorityQueue PQ = new PriorityQueue<Integer>();
// Checking whether PQ is empty or not
// by calling isEmpty() method on PQ
System.out.println(PQ.isEmpty());
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4