Salesforce Apex collections are data structures that enable developers to manage and manipulate groups of related data efficiently. Collections are essential in Salesforce development because they facilitate operations on large datasets, such as retrieving, processing, and storing records. Apex supports three main types of collections: Lists, Sets, and Maps.
1. Lists in Apex
Definition:
A List is an ordered collection of elements that can include duplicates. Lists are indexed, allowing you to access, add, or modify elements using their positions.
Use Cases:
Storing and iterating through multiple records.
Maintaining ordered data.
Allowing duplicate values when necessary.
Syntax Example:
// Example of a List in Apex
List<String> fruits = new List<String>();
fruits.add('Apple');
fruits.add('Banana');
fruits.add('Apple'); // Duplicate allowed
System.debug('Fruits List: ' + fruits); // Output: [Apple, Banana, Apple]
// Accessing elements by index
String firstFruit = fruits[0]; // Output: Apple
System.debug('First Fruit: ' + firstFruit);
// Iterating through a List
for (String fruit : fruits) {
System.debug('Fruit: ' + fruit);
}
See also: Complete list of String Class Methods Apex in Salesforce
2. Sets in Apex
Definition:
A Set is an unordered collection of unique elements. Duplicate values are automatically ignored.
Use Cases:
Ensuring uniqueness within a dataset.
Efficiently checking membership or presence of elements.
Syntax Example:
// Example of a Set in Apex
Set<String> cities = new Set<String>();
cities.add('New York');
cities.add('Los Angeles');
cities.add('New York'); // Duplicate ignored
System.debug('Cities Set: ' + cities); // Output: {New York, Los Angeles}
// Check if a city exists in the Set
Boolean isPresent = cities.contains('Los Angeles'); // Output: true
System.debug('Is Los Angeles in the Set? ' + isPresent);
// Removing an element from the Set
cities.remove('New York');
System.debug('Updated Cities Set: ' + cities); // Output: {Los Angeles}
3. Maps in Apex
Definition:
A Map is a collection of key-value pairs where each key is unique, and each key maps to a single value.
Use Cases:
Storing and retrieving data using unique identifiers.
Associating records with specific keys.
Syntax Example:
// Example of a Map in Apex
Map<String, String> countryCapitalMap = new Map<String, String>();
countryCapitalMap.put('USA', 'Washington D.C.');
countryCapitalMap.put('India', 'New Delhi');
countryCapitalMap.put('France', 'Paris');
// Accessing a value by its key
String capitalOfIndia = countryCapitalMap.get('India');
System.debug('Capital of India: ' + capitalOfIndia); // Output: New Delhi
// Iterating through a Map
for (String country : countryCapitalMap.keySet()) {
System.debug('Country: ' + country + ', Capital: ' + countryCapitalMap.get(country));
}
// Removing a key-value pair
countryCapitalMap.remove('France');
System.debug('Updated Map: ' + countryCapitalMap);
See also: Map class in Salesforce Apex
Best Practices for Using Collections in Apex
Initialize Collections Properly: Always initialize lists, sets, and maps before adding or accessing elements.
Avoid Hard-Coding: Use dynamic data fetching instead of hard-coded values in collections.
Use Proper Data Structures: Choose the appropriate collection type based on requirements (e.g., use Sets for uniqueness and Maps for key-value associations).
Bulkify Code: When working with Salesforce objects, use collections to handle data in bulk to avoid hitting governor limits.
Conclusion
Collections in Apex—Lists, Sets, and Maps—are powerful tools that make managing and manipulating data seamless and efficient. By understanding when and how to use each collection type, you can write scalable and optimized Apex code. Whether you are handling large datasets, ensuring data uniqueness, or mapping key-value pairs, collections are indispensable for every Salesforce developer.
Top comments (0)