Till Java 7, the collections framework relied on the concept of external iteration, where a Collection provides, by implementing Iterator and clients use this to step sequentially through the elements of a collection. Below given examples to count the even integer in a list.
OR we can write like this:
Above mentioned code snippets are the example of external iteration. External iteration is straightforward enough, but it has several problems:
1) Java’s for-each loop/iterator is inherently sequential, and must process the elements in the order specified by the collection.
2) It limits the opportunity to manage the control flow, which might be able to provide better performance by exploiting reordering of the data, parallelism, short-circuiting, or laziness.
Sometimes the strong guarantees of the for-each loop (sequential, in-order) are desirable, but often are just disadvantage to performance. The alternative to external iteration is internal iteration, where instead of controlling the iteration, client let it handle by library and only provide the code which must be executed for all/some of data elements.
The internal-iteration equivalent of the previous example is: