How should I iterate a priority queue properly?
foreach, iterator, java, priority-queue, queue
Solution
Yes, if you need to check every single element in the collection, an `iterator` or `for each` is probably best.
Iterator<E> iter = myPriorityQueue.iterator();
while (iter.hasNext()) {
current = iter.next();
// do something with current
}
Or
for (Element e : myQueue) {
// do something with e
}
Problem
I have a java assignment involving iterating a priority queue. The queue consists of objects with a string and an int in them and i need to have a way to check a seperate object's string against all the objects in the queue. Would it be best way to do this be an iterator object? That seems too messy. I could dequeue and enqueue but that seems inefficient. Maybe a foreach loop?