Iterator Examples for Java, C++ and PHP

Updated November 5, 2020

An iterator is a tool used in computer programming that permits a programmer to transverse a class, data structure or abstract data type. Iterators are typically linked very tightly to the class, data structure or data type to which the iterators are intended to provide access. They differ depending upon the programming language used and their purpose.

Examples of Iterators

For the Java Platform

Oracle provides some iterator examples on their website that are intended to be used for the Java 2 platform:

public void remove()

This iterator removes the last element that the iterator returns from your underlying data collection. You may use this iterator only one time for each call to next. However, if you modify your underlying data collection while the iterator is in the process of working, then the behavior of the iterator is not specified in the code.

public Object next()

This iterator is used if you wish to have the next element from the iterator returned.


For C++

C Plus provides some examples of iterators that can be used in the C++ programming language. Iterators in C++ are any objects that can move through a data structure, class or data set by using operators to indicate how to sort through and select a specific element. The iterators can allow for random access; bidirectional access; forward access; input or output.

Some examples of iterators from C++ include:


This can be used as a random access iterator that is bidirectional and that allows for input and output. It also allows for equality comparisons and inequality comparisons.


This is a random access iterator. It supports the "+" and "-" arithmetic operators.

Boost, a C++ library, also provides iterators that can be used for various tasks. For example:

Counting Iterator

template <

class Incrementable

, class CategoryOrTraversal = use_default

, class Difference = use_default


class counting_iterator



typedef Incrementable value_type;

typedef const Incrementable& reference;

typedef const Incrementable* pointer;

typedef /* see below */ difference_type;

typedef /* see below */ iterator_category;


counting_iterator(counting_iterator const& rhs);

explicit counting_iterator(Incrementable x);

Incrementable const& base() const;

reference operator*() const;

counting_iterator& operator++();

counting_iterator& operator--();


Incrementable m_inc; // exposition


Filter Iterator


class filter_iterator



typedef iterator_traits::value_type value_type;

typedef iterator_traits::reference reference;

typedef iterator_traits::pointer pointer;

typedef iterator_traits::difference_type difference_type;

typedef /* see below */ iterator_category;


filter_iterator(Predicate f, Iterator x, Iterator end = Iterator());

filter_iterator(Iterator x, Iterator end = Iterator());



filter_iterator<Predicate, OtherIterator> const& t

, typename enable_if_convertible<OtherIterator, Iterator>::type* = 0 // exposition


Predicate predicate() const;

Iterator end() const;

Iterator const& base() const;

reference operator*() const;

filter_iterator& operator++();


Predicate m_pred; // exposition only

Iterator m_iter; // exposition only

Iterator m_end; // exposition only



For PHP provides iterator examples that can be used with the PHP computer programming language. These allow for many day-to-day tasks to be completed.

Examples of iterators for PHP include:

  • An array function that makes it possible to call back an iterated value.
  • A filter of iterators that allows you to filter out leaf nodes and return all directories and files.

So, now you have different examples of iterators for different types of computer programming languages. You can better see how iterators are written and how they differ from platform to platform depending upon the programming language used and the purpose of the iterator.