![]() |
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 in a NullPointerException. In this article, we will learn How to handle Null Elements in a PriorityQueue in Java. Handle Null Elements in a PriorityQueueWe cannot directly handle null elements in a PriorityQueue in Java. As per design, the PriorityQueue class does not allow adding null elements. The reasons are given below:
At last, if we want to filter those null values then it all depends on situations where we add the elements to the priority queue. Program to Handle Null Elements in a PriorityQueue in JavaOne of the situations is like if we want to add the List elements (contains NULL values) into the priority queue then we can filter it out by shifting the elements list to the priority queue and based on priority it will handle all the null elements. Java
Output
[1, 3, 4, 5, 6] Explanation of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |