PriorityQueue是否允许重新排序队列中已有的元素? [英] Does a PriorityQueue allow elements already within the queue to be reordered?
问题描述
我希望增加或降低 PriorityQueue
中项目的优先级:例如,我可能正在下载一长串图片,突然想要第三十个图片最高优先级。
I want to augment or lower the priority of items in a PriorityQueue
: for example, I might be downloading a long list of images and suddenly want the thirtieth one to have highest priority.
据我所知, poll()
始终返回值最小的队列对象(如由比较器确定)。如果我可以降低队列中已有项的值(例如,如果该值由对象中的 int
确定,我会减少 int
某些其他函数中的值),它将首先由 poll()
返回,还是允许 poll的排序()
在插入时完成此操作(例如,将新队列元素向下列入列表直到达到自然深度)?
As I understand it, poll()
always returns the queue object with the lowest value (as determined by a comparator). If I can lower the value of an item already in a queue (e.g. if this value is determined by an int
in the object and I reduce the int
value in some other function), will it be returned first by poll()
, or is the sorting that allows poll()
to do this done at insert time (e.g. by bubbling new queue elements down a list till they reach their "natural" depth)?
如果这是在 PriorityBlockingQueue
上完成的,是否会导致并发问题?
If this is done on a PriorityBlockingQueue
, could it cause concurrency issues?
推荐答案
如果更改确定其顺序的属性,则Java中的任何集合都不会自动重新排序元素。对于依赖于.hashCode(),. equals()或某个比较器的集合,不允许在对象驻留在集合中时更改该对象,以便hashcode / equals或比较器产生不同的值。
None of the collections in Java automatically reorders elements if you change the property that determine their order. For collections that depend on the .hashCode() , .equals() or some comparator, you are not allowed to change the object while it resides in the collection so that the hashcode/equals or comparator would yield different values.
如果要在PriorityQueue中更改其优先级,则必须删除,更改,重新插入对象。
You have to remove, change, re-insert the object if you want to change its priority within a PriorityQueue.
这篇关于PriorityQueue是否允许重新排序队列中已有的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!