Priority Queues
Last updated
Last updated
Priority Queue is an extension of queue with following properties.
Every item has a priority associated with it.
An element with high priority is dequeued before an element with low priority.
If two elements have the same priority, they are served according to their order in the queue.
Few important points on Priority Queue are as follows:
PriorityQueue doesn’t permit null.
We can’t create PriorityQueue of Objects that are non-comparable
PriorityQueue are unbound queues.
The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements — ties are broken arbitrarily.
Since PriorityQueue is not thread-safe, so java provides PriorityBlockingQueue class that implements the BlockingQueue interface to use in java multithreading environment.
The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.
It provides time for add and poll methods.
It inherits methods from AbstractQueue, AbstractCollection, Collection and Object class.