You can use your own tree, which allows you to duplicate elements instead of heaps. However, you can use TreeMap<PriorityKey, LinkedHashSet<Value>>. All operations: O (log priorityType):
- add
get(priority).add(value)if get returns nullput(priority, new LinkedHashSet()) - deleting an arbitrary element is similar, except for the need to remove priority from the map if the set is empty.
- poll () -
-
Entry<PriorityKey, LinkedHashSet<Value>> e = map.firstEntry();
if (e==null) return null;
Iterator<Value> i = e.getValue().iterator();
Value v = i.next();
i.remove();
if (!i.hasNext()) map.remove(e.getKey());
return v;
However, if you need to change the priority you need to delete and add an item.
source
share