> For the complete documentation index, see [llms.txt](https://hanfak.gitbook.io/workspace/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hanfak.gitbook.io/workspace/languages/java/intermediate/collections/priority-queue.md).

# Priority queue

* An ordered queue
* Priority queues help consumers consume the higher priority messages first followed by the lower priority messages.
* Is heap data structure
* search is still O(N). On the other hand, It is best for finding min/max in a set O(1).
* add and remove is LogN, due to rebalancing

## vs TreeSet

* Tree set is red black tree
* <https://stackoverflow.com/questions/16540082/what-are-the-differences-between-heap-and-red-black-tree>

## link

* <https://www.freecodecamp.org/news/priority-queue-implementation-in-java/>
* <https://www.educba.com/priority-queue-vs-heap/>
* <https://cs.stackexchange.com/questions/105899/why-use-heap-over-red-black-tree>
