title: How to remove elements from a queue in Java with a loop
body: <p>I have a data structure like this:</p>
  
  <blockquote>
    <p>BlockingQueue mailbox = new LinkedBlockingQueue();</p>
  </blockquote>
  
  <p>I'm trying to do this:</p>
  
  <pre><code>for(Mail mail: mailbox)
  {
      if(badNews(mail))
      {
          mailbox.remove(mail);
      }
  }
  </code></pre>
  
  <p>Obviously the contents of the loop interfere with the bounds and a error is triggered, so I would normally do this:</p>
  
  <pre><code>for(int i = 0;  i &lt; mailbox.size(); i++)
  {
      if(badNews(mailbox.get(i)))
      {
          mailbox.remove(i);
          i--;
      }
  }
  </code></pre>
  
  <p>But sadly BlockingQueue's don't have a function to get or remove an element by index, so I'm stuck. Any ideas?</p>
  
  <p><em>Edit - A few clarifications:
  One of my goals is the maintain the same ordering so popping from the head and putting it back into the tail is no good. Also, although no other threads will remove mail from a mailbox, they will add to it, so I don't want to be in the middle of an removal algorithm, have someone send me mail, and then have an exception occur.</em></p>
  
  <p>Thanks in advance!</p>