🔁 Simplify Apex Loops with the Iterable Interface

When working with collections in Apex, most of us are used to looping like this:

for (String s : stringList) {
    System.debug(s);
}

But what if you want to make your own custom class behave like a list or set inside a for loop? That’s where the Iterable interface comes in.

In this post, we’ll walk through:
✅ A basic example with strings.
✅ A practical example with filtering.


1️⃣ Basic Example – MyIterable Class

Here’s the simplest implementation of Iterable<String>:

public class MyIterable implements Iterable<String> {
    private List<String> strings;

    public MyIterable(List<String> strings) {
        this.strings = strings;
    }

    public Iterator<String> iterator() {
        return strings.iterator();
    }
}

🔎 Key Points:

  • implements Iterable<String> → tells Apex this class can be looped in a for loop.
  • Stores the data (List<String>) inside.
  • Returns the list’s built-in iterator.

2️⃣ Test Class

@IsTest
public class MyIterableTest {
    @IsTest
    static void testIterableForLoop() {
        List<String> strings = new List<String>{'Hello', 'World'};

        MyIterable iterable = new MyIterable(strings);

        for (String str : iterable) {
            System.debug(str);
        }
    }
}

✅ Expected Debug Output:

Hello
World

This is the minimum working example to demonstrate Iterable in Apex.


3️⃣ Advanced Example – Filtering Out Nulls

Now, let’s take it a step further. Suppose you have a list of strings, and you only want to loop through non-null values. You can customize the iteration logic by creating your own Iterator class.

Iterable Class

public class FilteredIterable implements Iterable<String> {
    private List<String> strings;

    public FilteredIterable(List<String> strings) {
        this.strings = strings;
    }

    public Iterator<String> iterator() {
        return new FilteredIterator(strings);
    }
}

Iterator Class

public class FilteredIterator implements Iterator<String> {
    private List<String> strings;
    private Integer index = 0;

    public FilteredIterator(List<String> strings) {
        this.strings = strings;
    }

    public Boolean hasNext() {
        // Skip nulls
        while (index < strings.size() && strings[index] == null) {
            index++;
        }
        return index < strings.size();
    }

    public String next() {
        return strings[index++];
    }
}


4️⃣ Using the Advanced Iterable

List<String> words = new List<String>{'Hello', null, 'World', null, 'CloudWithAbhi'};
for (String word : new FilteredIterable(words)) {
    System.debug(word);
}

✅ Debug Output:

Hello
World
CloudWithAbhi


5️⃣ Why This Matters

  • The basic example (MyIterable) shows how to quickly wrap a collection.
  • The advanced example (FilteredIterable) shows how to control iteration behavior.

Real-world applications:

  • Skipping null/invalid records.
  • Chunking large data sets for batch processing.
  • Adding on-the-fly transformations during iteration.

🎯 Final Thoughts

The Iterable and Iterator interfaces unlock a new level of flexibility in Apex loops.

  • Start with a simple wrapper class ✅
  • Extend it to filter, transform, or manage large collections ✅

Whether you’re preparing for the Platform Developer certification or writing real-world code, understanding Iterable will help you write cleaner, reusable, and smarter Apex.