Curriculum
Collections are one of the most powerful and frequently used features in Apex programming. Collections allow developers to store and manage multiple values efficiently. Instead of creating separate variables for every record or value, collections provide structured containers that can hold large amounts of data and support advanced operations such as searching, sorting, filtering, and bulk processing.
In Salesforce development, collections are essential because most operations involve multiple records. Whether processing Accounts, Contacts, Leads, Opportunities, Students, Courses, or Payments, developers use collections to work with groups of records efficiently while staying within Salesforce Governor Limits.
Understanding Collections is critical for Salesforce Developers because they are used extensively in Apex Classes, Triggers, Batch Apex, Integrations, Lightning Components, and Flow Actions.
Collections are data structures used to store multiple values in a single variable.
Collections help developers:
Collections are fundamental to efficient Apex development.
Salesforce applications frequently work with:
Creating individual variables for each record would be impractical.
Collections provide a scalable solution.
Store multiple values together.
Reduce unnecessary processing.
Support bulk operations.
Simplify code structure.
Reduce repetitive coding.
These benefits make collections essential in Apex.
Apex supports three primary collection types.
Each collection serves a different purpose.
| Collection Type | Purpose |
|---|---|
| List | Ordered Collection |
| Set | Unique Collection |
| Map | Key-Value Collection |
Understanding when to use each collection is an important developer skill.
A List is an ordered collection that can store multiple values.
Characteristics:
Lists are the most commonly used collection type.
Syntax:
List<DataType> variableName =
new List<DataType>();
Example:
List<String> courses =
new List<String>();
This creates an empty list.
Example:
List<String> courses =
new List<String>();
courses.add('Salesforce');
courses.add('Python');
courses.add('Java');
The list now contains three values.
Example:
System.debug(courses);
Output:
(Salesforce, Python, Java)
Lists can hold multiple records efficiently.
Lists use index positions.
Example:
List<String> courses =
new List<String>{
'Salesforce',
'Python',
'Java'
};
System.debug(
courses[0]
);
Output:
Salesforce
Indexing starts from zero.
| Value | Index |
|---|---|
| Salesforce | 0 |
| Python | 1 |
| Java | 2 |
Understanding indexes is important when working with lists.
Example:
courses[1] = 'Data Science';
Result:
(Salesforce, Data Science, Java)
Lists allow value modification.
Example:
courses.remove(1);
Result:
(Salesforce, Java)
The element at index 1 is removed.
Size returns the number of elements.
Example:
System.debug(
courses.size()
);
Output:
2
Useful for validations and loops.
Example:
for(String course : courses){
System.debug(course);
}
Output:
Salesforce
Java
This is called a For-Each Loop.
Example:
List<Account> accounts =
[
SELECT Id, Name
FROM Account
];
Lists commonly store Salesforce records.
A Set is an unordered collection that stores unique values.
Characteristics:
Sets are useful when duplicates must be prevented.
Example:
Set<String> skills =
new Set<String>();
Creates an empty set.
Example:
skills.add('Java');
skills.add('Python');
skills.add('Java');
Result:
(Java, Python)
Duplicate values are automatically removed.
Sets are useful for:
These capabilities improve performance.
Example:
Boolean exists =
skills.contains('Python');
System.debug(exists);
Output:
true
Useful for validation logic.
Example:
skills.remove('Java');
Result:
(Python)
Values can be removed easily.
Example:
System.debug(
skills.size()
);
Output:
1
Size returns the total number of unique values.
Example:
for(String skill : skills){
System.debug(skill);
}
Output:
Python
Sets support iteration similar to lists.
Set<Id> accountIds =
new Set<Id>();
Commonly used in Triggers and Bulk Processing.
A Map stores data as key-value pairs.
Characteristics:
Maps are widely used in Apex development.
Example:
| Key | Value |
|---|---|
| 101 | Rahul |
| 102 | Priya |
| 103 | Amit |
Each key uniquely identifies a value.
Example:
Map<Integer, String>
students =
new Map<Integer, String>();
Creates an empty map.
Example:
students.put(101, 'Rahul');
students.put(102, 'Priya');
students.put(103, 'Amit');
The map now contains three records.
Example:
System.debug(
students.get(101)
);
Output:
Rahul
Maps retrieve values using keys.
Example:
students.put(
101,
'Rahul Sharma'
);
The existing value is updated.
Example:
students.remove(102);
The key-value pair is removed.
Example:
Boolean exists =
students.containsKey(101);
Output:
true
Useful for validations.
Example:
System.debug(
students.size()
);
Output:
2
Returns total entries.
Example:
for(Integer key :
students.keySet()){
System.debug(
students.get(key)
);
}
Output:
Rahul Sharma
Amit
Maps support iteration through keys.
Example:
Map<Id, Account>
accountMap =
new Map<Id, Account>(
[
SELECT Id, Name
FROM Account
]
);
This is one of the most common Apex patterns.
Collections help reduce:
Bulkified code relies heavily on collections.
Bad Practice:
for(Account acc : Trigger.new){
insert new Contact();
}
Good Practice:
List<Contact> contacts =
new List<Contact>();
for(Account acc : Trigger.new){
contacts.add(
new Contact()
);
}
insert contacts;
Collections improve performance and scalability.
| Requirement | Collection |
|---|---|
| Ordered Data | List |
| Unique Values | Set |
| Key-Value Pairs | Map |
Selecting the correct collection improves code quality.
These practices support efficient Apex development.
Developers should choose collections carefully.
A software training company processes student enrollments.
List<String> students =
new List<String>{
'Rahul',
'Priya',
'Amit'
};
Set<String> courses =
new Set<String>{
'Salesforce',
'Python'
};
Map<Integer, String>
batchMap =
new Map<Integer, String>{
101 => 'Morning Batch',
102 => 'Evening Batch'
};
Collections help organize and process large amounts of information efficiently.
Understanding Collections helps professionals:
Collections are among the most important Apex concepts.
Collections are data structures used to store and manage multiple values efficiently in Apex. Salesforce supports three primary collection types: Lists for ordered data, Sets for unique values, and Maps for key-value relationships. Collections are essential for bulk processing, performance optimization, Governor Limit compliance, and scalable Salesforce development.
Collections are data structures used to store multiple values in a single variable.
List, Set, and Map.
List allows duplicate values.
Set automatically removes duplicate values.
A Map stores data as key-value pairs.
They support bulk processing, improve performance, and help avoid Governor Limit issues.
Looking to learn more technologies and programming skills?
WhatsApp us