I'm looking for some implementation of PQ in Java which allows iteration in PQ order - top element first, next one next etc. I tried using TreeSet (which implements NavigableSet) but it causes one problem. In my case:
- I'm using Comparator for my objects
- priority changes due to some external actions
- if priority changes I know for which object, but I don't know it's previous priority
As a result to the last point - I can't find my element in TreeSet when I would like to update its priority:/ Do you happen to know: smart way to obey this? or some implementation of PQ that is iterable in "good" way? or should I create some linked data structure that will match objects with their positions in tree ?
UPDATE:
- concurrency is not an issue
- object can't be removed from TreeSet because it's priority changed so Comparator will evaluate differently and object won't be found in this data structure. Inserting is not a problem.
- I can't use
compareTo
method as this priority is not proper way to compare those objects. That is why I need to useComparator
POSSIBLE SOLUTION:
- create class
PrioritizedObject
which will be compared by priority and keep my object - use map: my object ->
PrioritizedObject
- keep
PrioritizedObject
in someNavigableSet
I would use this map to remove objects from NavigableSet
. And of course update it with new elements if I add something.
Problem is that I will have to wrap iterator from this NavigableSet
to get iterator returning my objects.
Is there any better solution?