When should I use a TreeMap over a PriorityQueue and vice versa?

后端 未结 8 2153
南旧
南旧 2020-12-23 11:46

Seems they both let you retrieve the minimum, which is what I need for Prim\'s algorithm, and force me to remove and reinsert a key to update its value. Is there any advanta

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 12:23

    One of the differences is that remove(Object) and contains(Object) are linear O(N) in a normal heap based PriorityQueue (like Oracle's), but O(log(N)) for a TreeSet/Map.

    So if you have a large number of elements and do a lot of remove(Object) or contains(Object), then a TreeSet/Map may be faster.

提交回复
热议问题