10

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?

Anon
  • 105
  • 1
  • 1
  • 6

3 Answers3

13

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
}
The111
  • 5,757
  • 4
  • 39
  • 55
  • foreach not workin, you must access using .poll() see: https://stackoverflow.com/a/25569705/8684705 – pi lak Jul 19 '22 at 17:47
  • you will not have the order using iterable – pi lak Jul 20 '22 at 10:10
  • @pilak no, `.poll()` is not the right choice for OP's use case. It removes items from the queue (and OP literally explained they were aware of that option but did not want to do it that way). You are correct it does not guarantee an order; OP did not ask for that guarantee. The [Javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html#poll()) is pretty clear about how to get such order: " If you need ordered traversal, consider using Arrays.sort(pq.toArray())." – The111 Jul 20 '22 at 23:04
  • yes my bad I miss read the original post, and interprete your answers with this :/ what I was looking for was how to iterate over such a collection and get element sorted, but that is not what is asking this post... – pi lak Jul 21 '22 at 10:50
5

One small detail: if there's any chance your queue could be modified during the loop, then both iterator and for each will cause a ConcurrentModificationException; if there's a chance the queue will get modified during the processing, you could use poll():

    Resource resource;
    while ((resource = resourceQueue.poll()) != null) {
        this.processIncludes(resourceQueue, resource);
    }
Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
1

If you don't care about ordering (in that case - why are you dealing with PriorityQueue?), use Iterator. If you want to iterate by priority, then see the advice from Javadoc:

If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Miljen Mikic
  • 14,765
  • 8
  • 58
  • 66
  • 1
    Exactly, it is based on a heap which does prohibit complete ordering in itself. Thus you should call poll, because this will cause the heap to sift the min/max up to the root. But for his requirement, ordering doesn't matter, so he could iterate by a for each loop. – Thomas Jungblut Dec 07 '12 at 08:10
  • Im told I have to use a priority queue. It doesnt matter though what order I traverse it. I just need to check each objects string to make sure it doesnt match my seperate object's string – Anon Dec 07 '12 at 17:40