-
Enhancement
-
Resolution: Won't Fix
-
P4
-
None
-
5.0, 6
A DESCRIPTION OF THE REQUEST :
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())
System.out.println(integer);
JUSTIFICATION :
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())
System.out.println(integer);
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
If I pass an iterator as the argument to a for/in loop after the colon, it should just work.
ACTUAL -
I get a compile error when I try this now
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())
System.out.println(integer);
JUSTIFICATION :
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())
System.out.println(integer);
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
If I pass an iterator as the argument to a for/in loop after the colon, it should just work.
ACTUAL -
I get a compile error when I try this now
- duplicates
-
JDK-4988624 New for loop should also accept iterators as their object.
-
- Closed
-
-
JDK-6360734 Enhanced for loop should support iterators directly
-
- Closed
-
-
JDK-8181399 for-each loop should support Iterator itself
-
- Closed
-
- relates to
-
JDK-5070978 Allow to use CharSequence in foreach loop
-
- Closed
-
-
JDK-6341875 New for loop should treat null as an empty list
-
- Closed
-
-
JDK-6349852 Support java.util.Enumeration in enhanced for loop
-
- Closed
-
-
JDK-6402294 Enhance the new for loop to loop over 1 or more collections
-
- Closed
-
(2 relates to)