JDK-6312085 : enhanced-for statement (for-each loop) should support Iterator
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0,6
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • Submitted: 2005-08-17
  • Updated: 2017-06-01
  • Resolved: 2011-11-01
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Relates :  
Relates :  
Currently, the for/in statement  (ie for(Date date : collection)...) only supports Iterable and Object[] as the source of objects to iterate over.  There is no reason why this statement should not support Iterators directly.  For instance, the following code should work

List<Integer> myList = buildIntegers();
for (Integer integer : myList.iterator())

Sometimes I will have a method other than iterator() that returns the iterator I want to travers.  For instance, I could have a method that returns an iterator over a subset of objects in the source object...

Iterator<Integer> getOddIntegers();

Right now, I'm forced to use old style for loops with iterators.  I'd like to be able to type

for (Integer integer : myList.getOddIntegers())

If I pass an iterator as the argument to a for/in loop after the colon, it should just work.
I get a compile error when I try this now

Since Java 8, one can use a lambda instead, the example for (Integer integer : myList.getOddIntegers()) System.out.println(integer); can be written like this: myList.getOddIntegers().forEachRemaining(integer -> { System.out.println(integer); });

EVALUATION Proposals for new features in the Java programming language are no longer being accepted or maintained in the bug database at http://bugs.sun.com/. This also applies to proposals for new features in the Java Virtual Machine (that is, in the abstract JVM, as opposed to a JVM implementation like HotSpot). All proposals for new features should be made through the "JDK Enhancement - Proposal & Roadmap Process" described at http://openjdk.java.net/jeps/1. This specific request to change the Java programming language is related to the work of OpenJDK Project Lambda. It will be considered further by that project, not here. The bug database continues to accept and maintain submissions about technical errors in the design and specification of the Java programming language and the Java Virtual Machine.

EVALUATION The enhanced-for loop is syntactic sugar, and is meant to be Really Simple. Hiding the loop variable and the iterator object keeps the body safe and efficient. For example, the compiler can generate a loop variable that's final. And there's no chance of a concurrent thread turning the iterator to null or busily calling remove() during the iteration. And there is no side-effect of the loop exhausting the Expression's Iterator. From the JSR201 FAQ: --- Why can't I use the enhanced for statement with an Iterator (rather than an Iterable or array)? Two reasons: (1) The construct would not provide much in the way on syntactic improvement if you had an explicit iterator in your code, and (2) Execution of the loop would have the "side effect" of advancing (and typically exhausting) the iterator. In other words, the enhanced for statement provides a simple, elegant, solution for the common case of iterating over a collection or array, and does not attempt to address more complicated cases, which are better addressed with the traditional for statement. --- Also, by supporting Iterable<U> only, there is no question of what to do if the Expression after the : implements both Iterable<T> and Iterator<U>.