问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/44762616/does-priorityqueue-heapify-itself-if-any-object-in-the-collection-is-mutated-so