Collections Overview
Learning Content
Last updated: July 21, 2026
Collections Support in Apex (List, Set & Map)
Salesforce Apex provides powerful Collection data structures that allow developers to store and process multiple values efficiently. Instead of creating individual variables for every record, Collections enable you to group related data together, making your code cleaner, faster, and easier to maintain.
The three primary Collection types in Apex are List, Set, and Map. These collections are widely used in Apex classes, triggers, batch jobs, integrations, and SOQL queries.
💡 LearnFrenzy Insight
Imagine you're managing a classroom.
- A List is like an attendance register where students are recorded in order.
- A Set is like a unique guest list where duplicate names are not allowed.
- A Map is like a dictionary where every word has a unique meaning associated with it.
Why Do We Need Collections?
Business applications rarely process a single record at a time. For example, updating hundreds of Accounts, retrieving thousands of Opportunities, or processing multiple Cases requires a way to store and manipulate groups of records efficiently.
Collections help developers write bulkified Apex code, which is essential for working within Salesforce Governor Limits.
| Collection | Purpose | Allows Duplicates | Maintains Order |
|---|---|---|---|
| List | Stores an ordered collection of values. | Yes | Yes |
| Set | Stores unique values. | No | No |
| Map | Stores key-value pairs. | Keys must be unique. | Access by Key |
Simple Collection Examples
List Example
List<String> fruits =
new List<String>{
'Apple',
'Mango',
'Orange'
};
System.debug(fruits);
Debug Log Output
USER_DEBUG [6]|DEBUG|(Apple, Mango, Orange)
Set Example
Set<String> cities =
new Set<String>{
'Delhi',
'Mumbai',
'Delhi'
};
System.debug(cities);
Debug Log Output
USER_DEBUG [6]|DEBUG|{Delhi, Mumbai}
Notice that the duplicate value Delhi is automatically removed.
Map Example
Map<Integer,String> students =
new Map<Integer,String>{
1=>'Rahul',
2=>'Amit'
};
System.debug(students);
Debug Log Output
USER_DEBUG [6]|DEBUG|{1=Rahul, 2=Amit}
Real Business Scenario
🏢 Business Example
Suppose a sales manager updates 500 Opportunities at the same time.
Instead of processing each record individually, Apex retrieves all Opportunities into a List, removes duplicate IDs using a Set, and stores Account IDs with Account records in a Map for fast lookup.
This approach improves performance and helps avoid Salesforce Governor Limit violations.
Where Are Collections Used?
| Salesforce Feature | Collection Used |
|---|---|
| SOQL Query Results | List |
| Trigger.new | List |
| Removing Duplicate IDs | Set |
| Lookup Records by Id | Map |
| Batch Apex | List / Map |
| Bulk Data Processing | List, Set & Map |
✅ Best Practice
Whenever you process multiple Salesforce records, use Collections instead of individual variables. This makes your Apex code bulk-safe, easier to maintain, and compliant with Governor Limits.
🎯 Interview Tip
Question: What are the three Collection types available in Apex?
Answer: Apex provides three Collection types: List, which stores ordered values; Set, which stores unique values; and Map, which stores key-value pairs for fast data retrieval.
📚 Learn More
This section provides only an introduction to Apex Collections.
In Chapter 18 – Collections in Apex, you'll learn:
- List Methods
- Set Methods
- Map Methods
- Nested Collections
- Collections with SOQL
- Collections in Triggers
- Bulkification Techniques
- Performance Optimization
- 50+ Practical Examples
Practice What You've Learned
Test your understanding with these practice exercises