Newer Version Available
Custom Iterators
Using the Iterator interface you can create a custom set of instructions for traversing a List through a loop. This is useful for data that exists in sources outside of Salesforce that you would normally define the scope of using a SELECT statement. Iterators can also be used if you have multiple SELECT statements.
Using Custom Iterators
To use custom iterators, you must create an Apex class that implements the Iterator interface.
| Name | Arguments | Returns | Description |
|---|---|---|---|
| hasNext | Boolean | Returns true if there is another item in the collection being traversed, false otherwise. | |
| next | Any type | Returns the next item in the collection. |
All methods in the Iterator interface must be declared as global or public.
Using Custom Iterators with Iterable
If you do not want to use a custom iterator with a list, but instead want to create your own data structure, you can use the Iterable interface to generate the data structure.
| Name | Arguments | Returns | Description |
|---|---|---|---|
| iterator | Iterator class | Returns a reference to the iterator for this interface. |
The iterator method must be declared as global or public. It creates a reference to the iterator that you can then use to traverse the data structure.