I have a PriorityQueue that has an element with a priority. Now I want to add the same element again with a different priority and keep only the one with higher priority. I thought of checking the new element against the already present one and then deciding whether to keep the old one or replace, but I can’t find a way to compare my new element against an arbitrary element from the PriorityQueue.
I have a PriorityQueue that has an element with a priority. Now I want
Share
a
PriorityQueuewas not meant to access an arbitrary element in it, it is designed to allow fast access to the head alone. If you need to do this operation frequently, probably ajava.util.TreeSetwill be a better data structure.However, you can access any element by iterating
PriorityQueue[using anIterator] and breaking when you find your match. You cannot get performance better thenO(n)for getting an arbitrary element in any case for aPriorityQueue, because it was not designed to do it.