Priority queue java data structure12/15/2023 If the number of elements exceeds the memory allocated for the JVM process, our application will crash due to an OutOfMemoryError exception. The PriorityQueue is a dynamic data structure that has no limitation on the number of elements it can accept from producers. The head of the PriorityQueue will always have the smallest element based on natural ordering or comparator, the rear of the PriorityQueue will have the greatest element based on natural ordering or comparator. In case a custom order is required then we can pass a Comparator at construction time. The elements of the PriorityQueue are ordered as per the natural ordering. PriorityQueue Ordering and Implementation PriorityQueue Use-CasesĪ real-life example of a PriorityQueue is the hospital queue where all the patients are treated/admitted based on the criticality of the condition, patients with emergencies would be given priority over patients who came for regular health check-ups.Īnother notable example is the Operating System’s CPU scheduling which uses the PriorityQueue internally and assigns CPU-Cycles based on the priority of the threads. We will see the properties of Binary Heap later in this article. The PriorityQueue is based on the Binary Heap data structure. It is used when we don’t want our queue to follow the traditional F.I.F.O (first-in-first-out) ordering but instead gives preference to the higher priority elements. To illustrate the advantages we get from PriorityBlockingQueue, let’s compare it to the traditional PriorityQueue first.Ī PriorityQueue in Java is a special type of queue which is used when the elements are required to be processed based on their priority.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |