jargon

Comparison

Internal vs external iteratorvsIterator pattern

Internal vs external iterator

one version hands you each element and you decide when to stop; the other takes your function and runs the loop itself.

An external iterator gives the caller control — next, hasNext, break out whenever — while an internal one takes a function and drives the loop itself, as forEach and map do. External iteration composes with early exit, zipping two sequences and resuming; internal iteration is shorter and lets the collection choose the traversal, including running it in parallel. The distinction explains why some collection APIs make an ordinary break impossible.

Full entry →

Iterator pattern

you walked the collection without knowing whether it was an array, a tree or a paged remote call.

Providing sequential access to the elements of a collection without exposing how it is stored. It is the clearest case of a pattern that became a language feature — foreach, iterators and generators are the pattern built in — which is why it is worth knowing mostly as history and as an interview example. The one live concern it still raises is what happens when the underlying collection changes mid-iteration.

Full entry →

Related comparisons