Search code examples
javapriority-queue

How does Java's PriorityQueue differ from a min-heap?


Why did they name PriorityQueue if you can't insertWithPriority? It seems very similar to a heap. Are there any differences? If no difference, then why was it named PriorityQueue and not Heap?


Solution

  • Add() works like an insertWithPriority.

    You can define priority for the type that you want using the constructor:

    PriorityQueue(int, java.util.Comparator)
    

    look under https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/PriorityQueue.html

    The order the Comparator gives will represent the priority in the queue.