title: Stack, foreach, wrong order?
body: <p>When using Java's <code>for</code>each syntax, <code>Stack</code> doesn't use LIFO ordering on the outputted elements. Consider the following code:</p>
  
  <pre><code>import java.util.Queue;
  import java.util.Stack;
  import java.util.LinkedList;
  
  public class QueueStackTest {
    private static int[] numbers = {1, 2, 3, 4, 5};
  
    public static void main(String[] args) {
      Stack&lt;Integer&gt; s = new Stack&lt;Integer&gt;();
      Queue&lt;Integer&gt; l = new LinkedList&lt;Integer&gt;();
  
      for (int i : numbers) {
        s.push(i);
        l.offer(i);
      }
  
      System.out.println("Stack: ");
      for(Integer i : s) {
        System.out.println(i);
      }
  
      System.out.println();
      System.out.println("Queue:");
      for(Integer i : l) {
        System.out.println(i);
      }
    }
  }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>Stack: 
  1
  2
  3
  4
  5
  
  Queue:
  1
  2
  3
  4
  5
  </code></pre>
  
  <p>Questions:</p>
  
  <ol>
  <li>Does this make sense? Is it a bug?</li>
  <li>Can I guarantee that this will, at least, return Queue elements in the correct order?</li>
  <li>When consuming (processing) a <code>Stack</code> or a <code>Queue</code>, is this the best way to do it? Or should I make a more manual loop with something like: <code>while(!s.isEmpty()) { handle(s.pop()); }</code> or <code>while(!l.isEmpty()) { handle(l.poll()); }</code></li>
  </ol>