title: What are the benefits of the Iterator interface in Java?
body: <p>I just learned about how the Java Collections Framework implements data structures in linked lists. From what I understand, <code>Iterators</code> are a way of traversing through the items in a data structure such as a list. Why is this interface used? Why are the methods <code>hasNext()</code>, <code>next()</code> and <code>remove()</code> not directly coded to the data structure implementation itself?</p>
  
  <p>From the Java website: <a href="http://java.sun.com/javase/6/docs/api/" rel="noreferrer">link text</a></p>
  
  <blockquote>
    <p>public interface Iterator&lt;&#69;><p> An
    iterator over a collection. Iterator
    takes the place of Enumeration in the
    Java collections framework. Iterators
    differ from enumerations in two ways:</p>
    
    <p><ul><li>Iterators allow the caller to remove
    elements from the underlying
    collection during the iteration with
    well-defined semantics. <li>Method names
    have been improved.</ul> This interface is
    a member of the Java Collections
    Framework.</p>
  </blockquote>
  
  <p>I tried googling around and can't seem to find a definite answer. Can someone shed some light on why Sun chose to use them? Is it because of better design? Increased security? Good OO practice?</p>
  
  <p>Any help will be greatly appreciated. Thanks.</p>