如果集合中的任何对象发生了变异,以便它的键(在比较器中使用)发生了变化,PriorityQueue是否会自行堆砌? [英] Does PriorityQueue heapify itself if any object in the collection is mutated so that it's key(used in comparator) changes?
问题描述
我只想知道Java的 PriorityQueue
集合是否自动进行堆化,比较器中使用的任何键是否发生突变,或者我是否需要显式调用heapify来订购 binary树
?我正在尝试将数据结构用于诸如设置之类的缓存中,该数据结构在被引用后应立即将集合中的对象移到头部.
I just want to know if Java's PriorityQueue
collection automatically heapifies, if any of the keys used in the comparator is mutated, or do i need to explicitly call heapify, to order the binary tree
?
I am experimenting with the data structure to use it in a cache like setting, where an object in the collection should be immediately moved to the head, as soon as it is referenced.
推荐答案
当队列本身发生变异的操作(即 offer
, poll)时,队列会自动 heapifying
,删除
和 add
.
The queue automatically heapifies on operations that mutates the queue itself, namely offer
, poll
, remove
and add
.
如果您对元素进行了突变,则必须将其删除并重新插入.优先级队列无法得知元素已更新.
If you mutate an element you have to remove and re-insert it. The priority queue has no way of knowing that an element has been updated.
这篇关于如果集合中的任何对象发生了变异,以便它的键(在比较器中使用)发生了变化,PriorityQueue是否会自行堆砌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!