Skip to main content
Join us at TDX in San Francisco or on Salesforce+ on March 5-6 for the Developer Conference for the AI Agent Era. Register now.

Get Hands-On with an Iterable Variable in For Loops

Learning Objectives

After completing this unit, you’ll be able to:

  • Use an iterable variable in a for loop.
  • Implement an Iterable Interface in Apex.

Iterate Within For Loops More Easily with Iterable Variable

You can now easily iterate through lists or sets using an Iterable variable in a for loop. Instead of writing complex loops, developers can use this feature to simplify working with for loops that repeat a block of code in each item in a collection. Standardize how you loop through lists and sets to make your code more consistent.

Implement and Test an Iterable Interface in Apex

In this hands-on challenge, you create a custom Apex class that implements the Iterable. You then use this class to iterate over a collection of items within a for loop. This exercise explains how to simplify iteration in Apex using the Iterable interface.

It’s time to complete the hands-on challenge to maintain your Platform Developer I certification.

Resources

Hands-on Challenge

+500 points

Get Ready

You’ll be completing this unit in your own hands-on org. Click Launch to get started, or click the name of your org to choose a different one.

Your Challenge

Implement and Test Iteration Using the Iterable Interface in Apex
In this hands-on challenge, you will create a custom Apex class that implements the Iterable. You will then use this class to iterate over a collection of items within a for loop. This exercise will help you understand how to simplify iteration in Apex using the Iterable interface.

Prework:
  • Ensure you have access to a Salesforce Developer Org or a Trailhead Playground.
  • Familiarize yourself with basic Apex programming concepts.
  • Review the Salesforce documentation on the Iterable interfaces.
    • Create a custom Apex class:
      • Name: MyIterable
        • Implements: Iterable<String>
        • Class variable: private List<String> strings
    • Create a constructor for MyIterable:
      • Argument: List<String> strings
      • Initializes the class variable strings from the constructor’s argument strings
    • Implement the iterator method:
      • Return type: Iterator<String>
      • Returns an iterator for the strings list strings.iterator();
    • Create a test class:
      • Name: MyIterableTest
      • Annotation: @IsTest
    • Add a test method to the test class:
      • Name: testIterableForLoop
      • Type: void()
        • Annotation: @IsTest
    • Steps:
      • Create a list of strings: List<String> strings
      • Values: ‘Hello’,’World’
      • Create an instance of MyIterable with the list of strings.
      • Use a for loop to iterate over the MyIterable instance.
      • Print each string using System.debug.
    • Run the test class:
      • Open the Developer Console
      • Navigate to the Test menu
      • Select New Run
      • Select the MyIterableTest class and run the test
    • Verify the Output
      • Check the debug logs to verify that the for loop iterated over the collection and printed each string.
    • Expected Output:
      • The debug logs should display the following output:
        • DEBUG | Hello
        • DEBUG | World
    Share your Trailhead feedback over on Salesforce Help.

    We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

    Learn More Continue to Share Feedback