Checking all the elements with the iterator pattern
This iterator pattern can be close to the abstraction of a cursor pointing to the desired position. Since array construction is a commonly used structure type, this pattern was soon identified. It is considered one of the core patterns contained in the GoF’s book.
Motivation
The iterator pattern defines a transparent way to iterate through a collection of objects without having to expose or be aware of any of the internal details of an object. To step between elements, the iterator pattern uses an iteration function.
Finding it in the JDK
The java.base
module contains multiple implementations of the iterator pattern. The first implementation can be considered the one provided by the collection framework located in the java.util
package. An implementation of the Iterator
interface traverses through a collection without knowing about the element’s membership.
Another example that can be considered is the...