Salesforce Apex provides powerful collection types—Lists, Sets, and Maps—that allow developers to efficiently manage and manipulate data. These collection structures are some of the most widely used parameterized classes that you use day to day when doing Apex development in Salesforce. Understanding these collection types is crucial for optimizing Apex code performance and ensuring scalability. In this post, we’ll explore their characteristics, differences, and real-world use cases.
Lists: Ordered Collections with Duplicates
Characteristics:
- Ordered collection of elements.
- Allows duplicate values.
- Elements are accessible by index (zero-based).
Common Methods:
List<String> names = new List<String>();
names.add('Alice');
names.add('Bob');
names.add('Alice'); // Duplicate allowed
System.debug(names.get(0)); // Outputs: Alice
System.debug(names.size()); // Outputs: 3
Real-World Example:
Consider a scenario where you need to retrieve a list of Contact records based on an Account ID:
public List<Contact> getContactsByAccount(Id accountId) {
List<Contact> contacts = [SELECT Id, Name FROM Contact WHERE AccountId = :accountId];
return contacts;
}
Sets: Unique Collections without Duplicates
Characteristics:
- Unordered collection.
- Does not allow duplicate values.
Common Methods:
Set<String> emails = new Set<String>();
emails.add('alice@example.com');
emails.add('bob@example.com');
emails.add('alice@example.com'); // Ignored, as it’s a duplicate
System.debug(emails.size()); // Outputs: 2
Real-World Example:
If you need to collect unique email addresses from a list of Leads:
Set<String> uniqueEmails = new Set<String>();
for (Lead l : [SELECT Email FROM Lead WHERE Email != NULL]) {
uniqueEmails.add(l.Email);
}
System.debug(uniqueEmails);
This ensures that no duplicate email addresses exist in the Set.
Maps: Key-Value Pairs for Fast Lookup
Characteristics:
- Stores data as key-value pairs.
- Provides fast lookup and retrieval.
- Keys are unique, values can be duplicated.
Common Methods:
Map<String, Integer> productInventory = new Map<String, Integer>();
productInventory.put('Laptop', 10);
productInventory.put('Monitor', 5);
System.debug(productInventory.get('Laptop')); // Outputs: 10
Real-World Example:
Imagine you need to map Account IDs to their respective names for quick reference:
Map<Id, String> accountMap = new Map<Id, String>();
for (Account acc : [SELECT Id, Name FROM Account]) {
accountMap.put(acc.Id, acc.Name);
}
System.debug(accountMap);
This allows quick retrieval of an Account’s name using its ID without additional queries.
Performance Considerations
- Lists are best when order matters or duplicates are needed.
- Sets provide efficient storage and retrieval of unique values.
- Maps enable quick lookups and associations of related data.
Using the correct data structure for the right scenario ensures optimized memory usage and improved Apex performance.
Conclusion
Salesforce Apex collections—Lists, Sets, and Maps—are indispensable tools for efficient data handling. Understanding their strengths and use cases can drastically improve your code’s efficiency and maintainability. Start leveraging these collections today to optimize your Apex development.

We would love to hear your comments!