Java Reference
In-Depth Information
Listing 4.1. Collections: external iteration with a for-each loop
Note that the for-each hides some of the iteration complexity. The for-each construct is syntactic
sugar that translates into something much uglier using an Iterator object.
Listing 4.2. Collections: external iteration using an iterator behind the
scenes
Listing 4.3. Streams: internal iteration
Let's use an analogy to understand the differences and benefits of internal iteration. Let's say
you're talking to your two-year-old daughter, Sofia, and want her to put her toys away:
You: “Sofia, let's put the toys away. Is there a toy on the ground?”
Sofia: “Yes, the ball.”
You: “Okay, put the ball in the box. Is there something else?”
Sofia: “Yes, there's my doll.”
You: “Okay, put the doll in the box. Is there something else?”
Sofia: “Yes, there's my book.”
You: “Okay, put the topic in the box. Is there something else?”
Sofia: “No, nothing else.”
You: “Fine, we're finished.”
 
Search WWH ::




Custom Search