Lably
Skip to content

Iterator

Object that enables processing collection items in order

  • 41 min read
  • 7 chapters

Related on Lably

Through Computer programming, Object (computer science) and Interface (object-oriented programming)

A collection may provide multiple iterators via its interface that provide items in different orders, such as forwards and backwards.

An iterator is often implemented in terms of the structure underlying a collection implementation and is often tightly coupled to the collection to enable the operational semantics of the iterator.

An iterator is behaviorally similar to a database cursor.

Iterators date to the CLU programming language in 1974.

Pattern

An iterator provides access to an element of a collection (element access) and can change its internal state to provide access to the next element (element traversal). It also provides for creation and initialization to a first element and indicates whether all elements have been traversed. In some programming contexts, an iterator provides additional functionality.

An iterator allows a consumer to process each element of a collection while isolating the consumer from the internal structure of the collection. The collection can store elements in any manner while the consumer can access them as a sequence.

In object-oriented programming, an iterator class is usually designed in tight coordination with the corresponding collection class. Usually, the collection provides the methods for creating iterators.

A loop counter is sometimes also referred to as a loop iterator. A loop counter, however, only provides the traversal functionality and not the element access functionality.

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Computing and communication