for (Item next : collection) {
// do something with next
}
2
6
-1
8
-5
4
2
6
-1
8
-5
4
2
6
-1
8
-5
4
2
6
-1
8
-5
4
2
6
8
4
...
...
...
...
...
2
6
8
4
...
Not Good! 8 is skipped
Expected behaviour
Remove Negative Numbers
Image source: https://refactoring.guru/
Iterable<E>
Iterator<E> iterator()
<<interface>>
Iterator<E>
boolean hasNext()
E next()
void remove()
<<interface>>
returns new MyCollectionIterator<E>()
MyCollection<E>
Iterator<E> iterator()
MyCollectionIterator<E>