title: What are the benefits of using an iterator in Java
body: <p>I was browsing over the following code example:</p>
  <pre><code>public class GenericTest {
    public static void main (String[] args) {
      ArrayList&lt;String&gt; myList = new ArrayList&lt;String&gt;();
      String s1 = &quot;one&quot;;
      String s2 = &quot;two&quot;;
      String s3 = &quot;three&quot;;
  
      myList.add(s1); myList.add(s2); myList.add(s3);
  
      Iterator&lt;String&gt; itr = myList.iterator();
      String st;
  
      while (itr.hasNext()) {
        st = itr.next();
        System.out.println(st);
      }
    }
  }
  </code></pre>
  <p>I'm wondering what are the benefits of using an implementation of the <code>Iterator</code> interface instead of using a plain-old for-each loop?</p>
  <pre><code> for (String str : myList) {
     System.out.println(str);
   }
  </code></pre>
  <p>If this example is not relevant, what would be a good situation when we should use the Iterator?</p>