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
// 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()); } } |
false
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()); } } |
true
How to Check if a PriorityQueue is Empty or Contains Elements in Java?
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.