<p>In Java 5 and above you have the foreach loop, which works magically on anything that implements <code>Iterable</code>:</p>

<pre><code>for (Object o : list) {
  doStuff(o);
}
</code></pre>

<p>However, <code>Enumerable</code> still does not implement <code>Iterable</code>, meaning that to iterate over an <code>Enumeration</code> you must do the following:</p>

<pre><code>for(; e.hasMoreElements() ;) {
  doStuff(e.nextElement());
}
</code></pre>

<p>Does anyone know if there is a reason why <code>Enumeration</code> still does not implement <code>Iterable</code>?</p>

<p><strong>Edit:</strong> As a clarification, I'm not talking about the language concept of an <a href="http://en.wikipedia.org/wiki/Enumerated_type" rel="noreferrer">enum</a>, I'm talking a Java-specific class in the Java API called '<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Enumeration.html" rel="noreferrer">Enumeration</a>'. </p>

[{'answer_id': 27389, 'author': 'Blorgbeard', 'author_id': 369, 'author_profile': 'https://Stackoverflow.com/users/369', 'pm_score': 6, 'selected': True, 'text': "<p>Enumeration hasn't been modified to support Iterable because it's an interface not a concrete class (like Vector, which was modifed to support the Collections interface).</p>\n\n<p>If Enumeration was changed to support Iterable it would break a bunch of people's code.</p>\n"}, {'answer_id': 27441, 'author': 'dlinsin', 'author_id': 198, 'author_profile': 'https://Stackoverflow.com/users/198', 'pm_score': 3, 'selected': False, 'text': '<p>AFAIK Enumeration is <a href="http://java.sun.com/javase/6/docs/api/java/util/Iterator.html" rel="noreferrer">kinda "deprecated"</a>:</p>\n\n<blockquote>\n  <p>Iterator takes the place of\n  Enumeration in the Java collections\n  framework</p>\n</blockquote>\n\n<p>I hope they\'ll change the Servlet API with JSR 315 to use Iterator instead of Enumeration. </p>\n'}, {'answer_id': 38518, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 6, 'selected': False, 'text': '<p>It doesn\'t make sense for <code>Enumeration</code> to implement <code>Iterable</code>. <code>Iterable</code> is a factory method for <code>Iterator</code>. <code>Enumeration</code> is analogous to <code>Iterator</code>, and only maintains state for a single enumeration.</p>\n\n<p>So, be careful trying to wrap an <code>Enumeration</code> as an <code>Iterable</code>. If someone passes me an <code>Iterable</code>, I will assume that I can call <code>iterator()</code> on it repeatedly, creating as many <code>Iterator</code> instances as I want, and iterating independently on each. A wrapped <code>Enumeration</code> will not fulfill this contract; don\'t let your wrapped <code>Enumeration</code> escape from your own code. (As an aside, I noticed that Java 7\'s <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/DirectoryStream.html" rel="noreferrer"><code>DirectoryStream</code></a> violates expectations in just this way, and shouldn\'t be allowed to "escape" either.)</p>\n\n<p><code>Enumeration</code> is like an <code>Iterator</code>, not an <code>Iterable</code>. A <code>Collection</code> is <code>Iterable</code>. An <code>Iterator</code> is not.</p>\n\n<p>You can\'t do this:</p>\n\n<pre><code>Vector&lt;X&gt; list = …\nIterator&lt;X&gt; i = list.iterator();\nfor (X x : i) {\n    x.doStuff();\n}\n</code></pre>\n\n<p>So it wouldn\'t make sense to do this:</p>\n\n<pre><code>Vector&lt;X&gt; list = …\nEnumeration&lt;X&gt; i = list.enumeration();\nfor (X x : i) {\n    x.doStuff();\n}\n</code></pre>\n\n<p>There is no <code>Enumerable</code> equivalent to <code>Iterable</code>. It could be added without breaking anything to work in for loops, but what would be the point? If you are able to implement this new <code>Enumerable</code> interface, why not just implement <code>Iterable</code> instead?</p>\n'}, {'answer_id': 46674, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 6, 'selected': False, 'text': '<p>As an easy and <strong>clean</strong> way of using an Enumeration with the enhanced for loop, convert to an ArrayList with java.util.Collections.list.</p>\n\n<pre><code>for (TableColumn col : Collections.list(columnModel.getColumns()) {\n</code></pre>\n\n<p>(javax.swing.table.TableColumnModel.getColumns returns Enumeration.)</p>\n\n<p>Note, this may be very slightly less efficient.</p>\n'}, {'answer_id': 31172283, 'author': 'user5071455', 'author_id': 5071455, 'author_profile': 'https://Stackoverflow.com/users/5071455', 'pm_score': 2, 'selected': False, 'text': '<p>If you would just like it to be syntactically a little cleaner, you can use:</p>\n\n<pre><code>while(e.hasMoreElements()) {\n  doStuff(e.nextElement());\n}\n</code></pre>\n'}, {'answer_id': 72718011, 'author': 'Clement Cherlin', 'author_id': 4455546, 'author_profile': 'https://Stackoverflow.com/users/4455546', 'pm_score': 0, 'selected': False, 'text': '<p>It is possible to create an Iterable from any object with a method that returns an Enumeration, using a lambda as an adapter. In Java 8, use Guava\'s static <a href="https://guava.dev/releases/snapshot-jre/api/docs/com/google/common/collect/Iterators.html#forEnumeration(java.util.Enumeration)" rel="nofollow noreferrer"><code>Iterators.forEnumeration</code></a> method, and in Java 9+ use the Enumeration instance method <a href="https://docs.oracle.com/javase/9/docs/api/java/util/Enumeration.html#asIterator--" rel="nofollow noreferrer"><code>asIterator</code></a>.</p>\n<p>Consider the Servlet API\'s <code>HttpSession.getAttributeNames()</code>, which returns an <code>Enumeration&lt;String&gt;</code> rather than an <code>Iterator&lt;String&gt;</code>.</p>\n<p><strong>Java 8 using Guava</strong></p>\n<pre class="lang-java prettyprint-override"><code>Iterable&lt;String&gt; iterable = () -&gt; Iterators.forEnumeration(session.getAttributeNames());\n</code></pre>\n<p><strong>Java 9+</strong></p>\n<pre class="lang-java prettyprint-override"><code>Iterable&lt;String&gt; iterable = () -&gt; session.getAttributeNames().asIterator();\n</code></pre>\n<p>Note that these lambdas are truly Iterable; they return a fresh Iterator each time they are invoked. You can use them exactly like any other Iterable in an enhanced <code>for</code> loop, <code>StreamSupport.stream(iterable.spliterator(), false)</code>, and <code>iterable.forEach()</code>.</p>\n<p>The same trick works on classes that provide an Iterator but don\'t implement Iterable. <code>Iterable&lt;Something&gt; iterable = notIterable::createIterator;</code></p>\n'}]
['https://Stackoverflow.com/questions/27240', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1666/']