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

前端 未结 6 1920
时光取名叫无心
时光取名叫无心 2021-01-30 00:44

Why did they name PriorityQueue if you can\'t insertWithPriority? It seems very similar to a heap. Are there any differences? If no difference, the

6条回答
  •  闹比i
    闹比i (楼主)
    2021-01-30 01:18

    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.

提交回复
热议问题