JDK-8181399 : for-each loop should support Iterator itself
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • Submitted: 2017-05-31
  • Updated: 2017-06-01
  • Resolved: 2017-06-01
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
The for-each-loop currently only supports arrays and instances of Iterable. There's no reason, why it shouldn't also support the Iterator itself directly.

JUSTIFICATION :
It can be nasty and expensive to wrap an existing iterator with a dummy iterable just to use the nice and handy for each loop.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Iterator it = ...;

for ( Object o : it )
{
}


CUSTOMER SUBMITTED WORKAROUND :
DummyIterable itbl = new DummyIterable( it );
for ( Object o : itbl )
{
}

// or:

while ( it.hasNext() )
{
    Object o = it.next();
}