How and Where to Use Iterators

How to use Iterators

Cells Iterator

There are various ways to access the cells iterator, and one can use any of these methods based on the application requirements. Here are the methods that return the cells iterator.

  1. Cells.iterator
  2. Row.iterator
  3. Range.iterator

All of the above‑mentioned methods return an iterator that allows traversal of the collection of cells that have been initialized.

The following code example demonstrates the implementation of the Iterator class for a cells collection.

Rows Iterator

The Rows Iterator can be accessed via the RowCollection.iterator method. The following code example demonstrates the implementation of the Iterator for the RowCollection class.

Columns Iterator

The Columns Iterator can be accessed via the ColumnCollection.iterator method. The following code example demonstrates the implementation of the Iterator for the ColumnCollection class.

Where to use Iterators

In order to discuss the advantages of using iterators, let’s take a real‑time example.

Scenario

An application requirement is to traverse all cells in a given worksheet to read their values. Several ways can be used to achieve this goal; a few are demonstrated below.

Using Display Range
Using MaxDataRow & MaxDataColumn

As you can observe, both of the above‑mentioned approaches use more or less similar logic, that is, loop over all cells in the collection to read the cell values. This could be problematic for a number of reasons, as discussed below.

  1. APIs such as MaxRow, MaxDataRow, MaxColumn, MaxDataColumn & MaxDisplayRange require extra time to gather the corresponding statistics. In case the data matrix (rows × columns) is large, using these APIs could impose a performance penalty.
  2. In most cases, not all cells in a given range are instantiated. In such situations, checking every cell in the matrix is not as efficient as checking only the initialized cells.
  3. Accessing a cell in a loop as Cells.get(rowIndex, columnIndex) will cause all cell objects in a range to be instantiated, which may eventually cause an OutOfMemoryError.
Conclusion

Based on the above‑mentioned facts, the following are possible scenarios where iterators should be used.

  1. Read‑only access of the cell collection is required, i.e., the requirement is to only inspect the cells.
  2. A large number of cells are to be traversed.
  3. Only initialized cells/rows/columns are to be traversed.