List vs. Iterable: Understanding the Core Difference for Salesforce Apex

As Salesforce developers, we’re constantly working with data – retrieving records, manipulating lists, and presenting information effectively. In this article, we’re diving into a seemingly subtle but crucial distinction: the difference between List<String> and Iterable<String> in Apex.

They appear same?

Initially, the difference might seem negligible. If you’re simply iterating through a List<String> using a FOR loop, the results are identical to iterating through an Iterable<String>. Both will process each element in the sequence, one after the other, based on it’s index. But when you try to imagine the logic behind it which uses sequential index to loop/iterate the data in the STRING ARRAY/LIST is where things start making some sense.

Iterators and Sequences

The key lies in understanding what’s under the hoodList is a primary data structure in Apex, a built-in, highly optimised collection. It provides a rich set of functions specifically designed for list and array operations.

However, Iterable is an interface. It defines a contract/behaviour pattern – a way for any custom class to declare that it can be iterated/LOOPED over. Without getting bogged down in technical details, think of it this way: Iterable is the blueprint for iteration/accessing data in sequence, while List is a concrete implementation with some other features.

Fun fact: Did you know that SET data structure in APEX was not iterable before Apex version 58.0.

Crucially, every Iterable object has its own iterator. An iterator is an object that manages the sequence of elements within the collection. It provides methods to move to the next element, check if there are more elements, and potentially access the current element. The FOR loop, when used with an Iterable, relies on the iterator to deliver the sequence. Dynamic for loops are all about it.

More fun fact: Wonder how BATCH class sends 200 records per each batch execution in an ITERATION? Just like Strings, even CHUNK of records can be returned as one iteration. Example: for(List<Account> currentBatchOfAccounts: CustomIterator<Account>)

It provides YOU full control on how to iterate you target class not just in terms of sequence but also if you want to perform any special operations on those class instances during iterations.

Why Does This Matter?

Let’s consider a practical scenario: you need to retrieve all active subscriptions and present them in the order of their expiry date – those expiring soon should appear first. While a simple SOQL query would achieve this, it returns the records in a linear sequence determined by SOQL LIST standard iterator. What if you require more granular control over the iteration process? For example: Send only subscriptions under 1 ACCOUNT in a single iteration which cannot be more than 10 active subscriptions per account.

This is where defining your own Iterable and Iterator implementations comes into play. By creating a custom class that implements Iterable, you can dictate the exact order in which records are processed. This allows you to apply custom logic, such as sorting, filtering, or applying specific transformations during the iteration.

While List<String> and Iterable<String> might appear interchangeable at first glance, understanding the underlying differences – particularly the role of the iterator – is vital for advanced Apex development. By leveraging custom iterators, you gain the flexibility to tailor the iteration process to your specific requirements, opening up possibilities for more sophisticated data processing and presentation.

Have a great day!