Till Java 7, the collections framework relied on the concept of external iteration, where a Collection provides, by implementing Iterable, a means to enumerate its elements i.e. Iterator and clients use this to step sequentially through the elements of a collection.
For example, if we wanted to get all strings in uppercase, we would write
Above both code snippets are for 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.