Java: What's the preferable way to iterate over a set? : <p>I've been teached in college that one has to create an iterator to loop over a set. </p>

<pre><code>java.util.HashSet&lt;String&gt; set = new java.util.HashSet&lt;String&gt;();

set.add("Green");
set.add("Blue");
set.add("Yellow");
set.add("Orange");
set.add("Red");

Iterator it = set.iterator();

while (it.hasNext()) {
    String current = (String) it.next();

    System.out.println(current);
}
</code></pre>

<p>Now I've seen in the code of colleagues that there is a more simple way to accomplish it. Using a for-loop:</p>

<pre><code>for (String str : set) {
    System.out.println(str);
}
</code></pre>

<p>I asked myself why the approach with the for-loop wasn't showed in college.</p>

<p><strong>Has the approach with the for-loop disadvantages?</strong></p>

<p><strong>What's the preferable way to iterate over a set and why?</strong></p>
