How to Check if a PriorityQueue is Empty or Contains Elements in Java?
Last Updated :
15 Feb, 2024
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 Empty
The below programs illustrate the Java.util.PriorityQueue.isEmpty() method:
Example 1:
The program showing that the PriorityQueue is not Empty:
Java
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args)
{
PriorityQueue PQ = new PriorityQueue<Integer>();
PQ.add( 6 );
PQ.add( 4 );
PQ.add( 55 );
PQ.add( 1 );
System.out.println(PQ.isEmpty());
}
}
|
Example 2:
The program showing that the PriorityQueue is Empty:
Java
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args)
{
PriorityQueue PQ = new PriorityQueue<Integer>();
System.out.println(PQ.isEmpty());
}
}
|
Share your thoughts in the comments
Please Login to comment...