Does PriorityQueue heapify itself if any object in the collection is mutated so that it's key(used in comparator) changes?

时间秒杀一切 提交于 2019-12-02 06:25:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!