Menu
PHP BABA LOGO
  • Home
  • Interview Questions
    • PHP String Interview Question
    • PHP Array Interview Qutions
    • jQuery Interview Quetions
    • PHP Interviwe Question on php.ini file
    • htaccess File Interview Quetions
  • About
PHP BABA LOGO

Platform Developer I Certification Maintenance (Winter ’25)

Posted on July 16, 2025

Iterable Variable in For Loops

What Is the Iterable Interface?
In Apex, Iterable enables you to:
Create custom classes that return a collection.
Iterate on that class with a for loop, just like you would on a list or set.
This is useful when:
You require custom iteration logic (e.g., filtering, batching).
You prefer clean and consistent looping syntax.

Steps to Implement and Use Iterable in Apex

1. Create a Class That Implements Iterable<T>

public class MyIterable implements Iterable {
private List strings;

// Constructor that initializes the strings field with the provided list
public MyIterable(List<String> strings) {
    this.strings = strings;
}

// Iterator method that returns an iterator for the strings list
public Iterator<String> iterator() {
    return strings.iterator();
}
}

2. Create a Test Class





@IsTest
public class MyIterableTest {

@IsTest
static void testIterableForLoop() {
    // Step 1: Create a list of strings
    List<String> strings = new List<String>{'Hello', 'World'};

    // Step 2: Create an instance of MyIterable with the list of strings
    MyIterable myIterableInstance = new MyIterable(strings);

    // Step 3: Add a debug statement to verify test execution
    System.debug('Running the for loop...');

    // Step 4: Use a for loop to iterate over the MyIterable instance
    for (String str : myIterableInstance) {
        // Step 5: Print each string using System.debug
        System.debug('String: ' + str);
    }
}

}

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Docker
  • Git
  • Interview Questions
  • Laravel
  • Linux
  • php
  • Salesforce
  • Uncategorized

Recent Posts

  • Platform Developer I Certification Maintenance (Winter ’25)
  • Batch Class in Salesforce
  • Platform Developer I Certification Maintenance (Winter ’24) Get Hands-on with Bind Variables in a SOQL Query
  • How to check Salesforce current release version
  • Salesforce Certification Coupons / Vouchers

Recent Posts

  • Platform Developer I Certification Maintenance (Winter ’25) July 16, 2025
  • Batch Class in Salesforce January 25, 2024
  • Platform Developer I Certification Maintenance (Winter ’24) Get Hands-on with Bind Variables in a SOQL Query December 19, 2023
  • How to check Salesforce current release version January 12, 2023
  • Salesforce Certification Coupons / Vouchers July 19, 2022
©2025 The Tech Paper | Powered by Abhinav Trivedi