Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Taking a look at the javadoc, you should be able to use the "take" method.</p> <blockquote> <p>public E take() throws InterruptedException Description copied from interface: BlockingQueue Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.</p> </blockquote> <p>Just throw a for loop around the queue and voila! You can iterate with no problems. :)</p> <p><strong>Assuming the queue is strictly FIFO</strong></p> <p>You can then use "put" to put the item back into the queue if it is not the green shirt</p> <blockquote> <p>public void put(E e) Inserts the specified element into this priority queue. As the queue is unbounded, this method will never block.</p> </blockquote> <p>This way the non-green shirts will still be in the queue in order (once the loop is finished).</p> <p><strong>The way you have it setup</strong></p> <p>Since you are using a PriorityBlockingQueue, putting the objects right back in the queue could eventually lead to the same item being pulled off the queue each time around the loop. You would need to create a new queue to put the item back into once it is checked to see if it is a green shirt.</p> <p><strong>Example code</strong></p> <p>PriorityBlockingQueue queue = new PriorityBlockingQueue(); PriorityBlockingQueue backupqueue = new PriorityBlockingQueue();</p> <pre><code> queue.put("A"); queue.put("B"); queue.put("C"); queue.put("D"); List&lt;String&gt; saveList = new ArrayList&lt;String&gt;(); int initialSize = queue.size(); for(int i = 0; i &lt; initialSize; i++){ String element = queue.take(); if(element.equals("C")){ saveList.add(element); } else { backupqueue.put(element); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload