Welcome to Java4u

A Single Place for all Java Resources

Looking for something?

Subscribe to this blog!

Receive the latest posts by email.

.Just enter your email below if you want to subscribe!

Email

Friday, June 13, 2014

Java 8 : Internal vs. External Iteration



External iteration

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.


Internal iteration 

Sometimes the strong guarantees of the for-each loop (sequential, in-order) are desirable, but often are just an 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.

alphabets.forEach(l ->l.toUpperCase());
 


 


0 comments: