Skip to main content
Junte-se a nós na TDX em São Francisco ou no Salesforce+ nos dias 5 e 6 de maio e assista à Developer Conference for the AI Agent Era. Registre-se agora.

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

Desafio prático

+500 pontos

Prepare-se

Você concluirá este(a) unidade em sua própria organização prática. Clique em Iniciar para começar, ou clique no nome da sua organização para escolher uma diferente.

Seu desafio

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
    Compartilhe seu feedback do Trailhead usando a Ajuda do Salesforce.

    Queremos saber sobre sua experiência com o Trailhead. Agora você pode acessar o novo formulário de feedback, a qualquer momento, no site Ajuda do Salesforce.

    Saiba mais Continue compartilhando feedback