How to Check if a PriorityQueue is Empty or Contains Elements in Java? Last Updated : 15 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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()); } } Outputfalse 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()); } } Outputtrue Comment More infoAdvertise with us Next Article How to Check if a PriorityQueue is Empty or Contains Elements in Java? pradeep6036ymca Follow Improve Article Tags : Java Java Programs priority-queue java-priority-queue Java Examples +1 More Practice Tags : Javapriority-queue Similar Reads How to Copy Elements from One PriorityQueue to Another in Java? In Java, a priority queue is a data structure that allows the users to store data based on their priority. In this article, we will learn how to copy elements from one priority queue to another in Java. Example Input: PriorityQueue original ={1,2,3,4,5}Output: PriorityQueue Copy ={1,2,3,4,5}Copy ele 2 min read How to Handle Null Elements in a PriorityQueue in Java? In Java, a Priority Queue is an abstract data type similar to a regular queue or stack, but with a key difference like elements are dequeued based on their priority. Regarding the null value property, the PriorityQueue class does not allow null elements. Attempting to add a null element will result 3 min read How to Iterate over the Elements of a PriorityQueue in Java? In Java, a Priority Queue is a Data structure that allows the users to store data based on their priority so that the elements with the highest priority can be accessed in constant time. In this article, we will learn how to iterate over the elements of a PriorityQueue in Java. Example Input: Priori 2 min read How to Customize the Ordering of Elements in a PriorityQueue Using a Comparator in Java? A PriorityQueue is a data structure that allows elements to be processed based on their priority. By default, it orders elements according to their natural ordering (e.g., numeric values in ascending order). However, sometimes we need a different sorting order based on specific criteria. In this art 4 min read How to Remove the Top Element From a PriorityQueue in Java? A priority queue is a specialized type of queue, in which elements are given with priorities, and those with more priorities are served before elements with lower priorities. It's equivalent to a regular queue (first-in, first-out) but prioritizes urgency rather than arrival order. Remove the Top El 2 min read Like