The overall answer to this is that there is an Apex Batch Handler which controls batch class execution. You could think of it like a method that calls your start method, then launches threads which call your execute method and handles the result of those threads, and finally calls your finish method once all execute threads have completed. With this in mind:

  1. The handler mentioned may be implemented something like:

    Batchable<Account> myBatch = batchFromBatchExecuteCall;
    Iterator<Account> queryResult = myBatch.start().iterator();
    while(queryResult.hasNext())
    {
        List<Account> scope = new List<Account>{queryResult.next()};
        for(integer i = 1; i < scopeSize && queryResult.hasNext(); ++i)
        {
            scope.add(queryResult.next());
        }
        launchThread(myBatch.execute, scope);
    }
    

    Of course, the above is very simplified to what the handler likely actually has to do, but this should demonstrate how it can take an iterator and use it to execute scopes

  2. Both List and queryLocator already implement the iterable() interface, so the caller can just use the iterable method whether it gets back one of those types or a custom Iterable type.

Answer from IllusiveBrian on Stack Exchange
🌐
Medium
medium.com › @anuhyadigital › what-is-interface-iterator-in-apex-bbb987509d0c
What is Interface Iterator in Apex | by Anuhya digital | Medium
September 30, 2024 - They provide structure to the code, ... An Iterator is a design pattern in Apex that provides a way to access the elements of a collection sequentially without exposing its underlying representation....
People also ask

How do you create a Custom Iterator in Apex?
A custom iterator requires implementing both the Iterable and Iterator interfaces. Step 1: Implement the Iterable Interface: The Iterable interface requires the iterator() method, which returns an iterator instance. public Iterator iterator() Step 2: Implement the Iterator Interface: The Iterator interface controls traversal using three methods: hasNext() → Checks whether more items exist next() → Returns the next item in the sequence remove() → Removes the last returned item Together, these methods define how records are accessed, ordered, filtered, and processed during batch execution. T
🌐
grazitti.com
grazitti.com › home › blog › implementing batch apex using custom iterables
Implementing Batch Apex Using Custom Iterables
What is the difference between Database.QueryLocator and Iterator/Iterable in Batch Apex?
The main differences lie in scalability, memory handling, and processing flexibility. Let’s check:  Database.QueryLocator Can process up to 50 million records Retrieves records dynamically in chunks from the database Bypasses standard SOQL retrieval limits Best suited for large-scale Salesforce record processing Iterator/Iterable Limited by standard Apex governor limits (typically 50,000 records) Loads records or objects into heap memory before processing Supports custom iteration logic and non-standard data sources Better for complex processing scenarios that QueryLocator cannot handle In
🌐
grazitti.com
grazitti.com › home › blog › implementing batch apex using custom iterables
Implementing Batch Apex Using Custom Iterables
What are the limitations of using Iterators in Batch Apex?
While Iterables offer flexibility, they also introduce several operational risks. Heap Size Limitations: All records are loaded into memory before execution begins. Large collections can trigger: System.LimitException: Apex heap size exceeded No Native Recovery Support: QueryLocator-based jobs can resume chunk processing more reliably after failures. Iterator-based jobs do not support the same recovery behavior. Serialization Constraints: Batch Apex serializes data between transactions. If your iterator stores non-serializable objects, such as: – HTTPResponse – JSONParser – Open streams
🌐
grazitti.com
grazitti.com › home › blog › implementing batch apex using custom iterables
Implementing Batch Apex Using Custom Iterables
🌐
Thesalesforcedev
thesalesforcedev.in › 2023 › 03 › 15 › using-custom-iterator-iterable-interface-in-apex
Using Custom Iterator & Iterable Interface in Apex – Welcome to The Salesforce Dev
May 17, 2025 - It accesses these elements sequentially. You don’t need to know the underlying structure of the collection. In Apex, you can define your own custom iterators by implementing the Iterator interface.
🌐
Jamessimone
jamessimone.net › blog › joys-of-apex › lazy-iterators
Joys Of Apex: Lazy Iterators - James Simone
The idea for this article has been brewing since then. Their LazyIterator object uses the Decorator pattern to add functionality to the underlying iterators found on all Salesforce List objects 2 — reading through their codebase got me re-excited about working with collections in Apex.
🌐
Salesforce
help.salesforce.com › s › articleView
Iterate Within For Loops More Easily with Iterable | Salesforce Help
Iterable<String> stringIterator = new List<String>{'Hello', 'World!'}; for (String str : stringIterator) { System.debug(str); }
🌐
Grazitti Interactive
grazitti.com › home › blog › implementing batch apex using custom iterables
Implementing Batch Apex Using Custom Iterables
June 15, 2026 - Additional Processing and Filtering: If you need to perform additional processing or apply custom filtering logic to the records returned by a query, implementing Batch Apex using iterables provides the necessary flexibility. With iterables, you have full control over the iteration logic, allowing you to easily incorporate custom processing or filtering logic into your batch operation.
🌐
Wordpress
suhasrathod.wordpress.com › 2020 › 04 › 10 › how-to-use-custom-iterator
How to use custom iterator
April 10, 2020 - An iterator traverses through every item in a collection. For example, in a while loop in Apex, you define a condition for exiting the loop, and you must provide some means of traversing the collection, that is, an iterator.
Find elsewhere
🌐
SalesforceBlue
salesforceblue.com › home › batch apex implementation using iterables
Batch Apex Implementation Using Iterables - SalesforceBlue
August 7, 2022 - To use custom iterators, you must create an Apex class that implements the Iterator interface.
🌐
Blogger
salesforcecodes.blogspot.com › 2020 › 01 › how-to-use-custom-iterator-in-apex.html
Salesforce Codes: How to use custom iterator in apex batch class
Use a custom iterator defined using CustomIterable interface · You can just use standard apex collections like list as iterator.
🌐
YouTube
youtube.com › codeisha
Custom Iterator in Apex | Benefits, Methods & Code Example - YouTube
Learn everything about Custom Iterators in Apex:1. What is a Custom Iterator?2. Why should you use Custom Iterators?3. Where can you implement them?4. Key me...
Published: December 29, 2024
Views: 200
🌐
forceapozee
forceapozee.com › post › mastering-iteration-in-apex-using-iterable-for-simplified-loops
Mastering Iteration in Apex: Using Iterable for Simplified Loops
January 30, 2025 - IntroductionEfficient iteration is key to writing clean and maintainable Apex code. With the introduction of the Iterable interface, Salesforce developers can now iterate through lists and sets more seamlessly in for loops.
🌐
Salesforcefaqs
salesforcefaqs.com › home › custom iterator interface in salesforce apex
Custom Iterator Interface in Salesforce Apex
May 9, 2025 - These iterators help to go through ... In Salesforce Apex, iterators are generally used to loop through data collection, such as a list of records or a map of key-value pairs....
🌐
Nebula Consulting
nebulaconsulting.co.uk › home › insights › using lazy evaluation to write salesforce apex code without for-loops
Using Lazy Evaluation to Write Salesforce Apex Code ...
February 16, 2026 - Implementing a framework for these lazy functions in Apex is not all that hard, so I've published our code for it on Bitbucket. As mentioned above, it's all about iterators. The core class is LazyIterator , this uses the Decorator Pattern to add functionality to an iterator.
🌐
Abubakar Solutions
abubakarsolutions.com › mastering-batch-apex-handling-multiple-objects-with-custom-iterators
Mastering Batch Apex: Handling Multiple Objects with Custom Iterators – Abubakar Solutions
April 18, 2025 - The Iterable class returns an instance of our custom Iterator. 🔹 This allows Batch Apex to fetch records from multiple sources.