17

I have created a queue containing objects which I would like to iterate through in the order that they were placed within the queue (First object placed in queue, 2nd object placed in queue, 3rd object...)

I saw a way of doing this online but I'm not sure if this will guarantee that objects in the queue will be visited in the correct order?

for(MyObject anObject : queue){
    //do someting to anObject...

Thank you for your help.

3 Answers3

16

Implement your queue as a LinkedList. Then you can iterate over your objects in order they were inserted. You have to declare the type of object being insert into the queue so you won't get any errors. You can keep it as object and specify it as a queue of objects then your code above would work. See below.

Queue<Object> queue = new LinkedList<Object>();
// add your objects here
// EX: queue.add(new MyObject)

for(Object item : queue){
    System.out.println(item.toString());
}
5

It depends on which Queue implementation you use.

For example LinkedList guarantees that iterations will return elements in FIFO (insertion) order. This is because it implements the Deque interface.

But generally speaking it is not necessarily the case for other types of Queues.

The javadoc for Queue states:

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out).

It also adds:

Every Queue implementation must specify its ordering properties.

So you simply need to check the javadoc of the specific queue you are using and you should find your answer.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • I have defined my queue as follows: `Queue queue = new LinkedList`. Would this work the way I intended if I were to use the above code? As it isn't a `Prority Queue`. Thanks –  Jun 03 '13 at 10:46
  • LinkedList also implements Deque, which is FIFO. So your iteration order will correspond to the insertion order (if you insert 1 then 2, you will read 1 then 2 when iterating). – assylias Jun 03 '13 at 10:51
  • @Giri You can check the details of how the Queue methods apply to a Deque in the second table on this page: http://docs.oracle.com/javase/7/docs/api/java/util/Deque.html – assylias Jun 03 '13 at 10:54
  • This answer is a waste of space. What IS the way to do it? – Ojonugwa Jude Ochalifu Mar 15 '18 at 14:02
  • @ojonugwaochalifu There is no need to be aggressive - I have updated the answer (to include what was in the comment section). – assylias Mar 15 '18 at 14:29
  • 1
    Am sorry if my comment appeared aggressive, that wasn't the intention.I just feel your answer should have included an actual implementation. – Ojonugwa Jude Ochalifu Mar 15 '18 at 14:36
0

I think its better to use ArrayList in this case. Please try that. Why you want to use queue only?

Harish Kumar
  • 528
  • 2
  • 15
  • 1
    Schools sometimes require the use of Queues in projects. I know. Terrible. – Kelmikra Oct 15 '15 at 08:37
  • 6
    If you need a FIFO (first in first out) data structure, then a queue is the appropriate choice over an ArrayList. The reason an ArrayList may not be an appropriate data structure to use is because another developer (or even yourself if you forgot that you needed FIFO) could easily write code to start removing items from the middle of the ArrayList which would compromise the FIFO data structure that was originally required. You could obviously use an ArrayList as a FIFO data structure but that would have to be enforced by the developer and not the data type which was selected. – Moustache_Me_A_Question Dec 14 '16 at 03:41