When you’re writing Apex, working with a single value is usually pretty straightforward.
For example:
String accountName = 'Acme';
But Salesforce development rarely stops at one value.
What if you need to work with multiple Accounts? Or collect unique Account IDs? Or quickly find an Account using its ID?
Creating a separate variable for every value would obviously not be practical.
This is where Collections in Apex come in.
Apex provides three main types of collections:
- List
- Set
- Map
They all help us work with multiple values, but each one works a little differently.
Let’s take a look at them one by one.
What are Collections in Apex?
A collection allows us to store multiple values in a single variable.
Instead of creating separate variables:
String name1 = 'John';
String name2 = 'Sarah';
String name3 = 'David';
We can store all of them in a List:
List<String> names = new List<String>();
names.add('John');
names.add('Sarah');
names.add('David');
Now, all three names are stored inside the names variable.
Apex has three main collection types:
List
Used when you need an ordered collection of values.
Set
Used when you need a collection of unique values.
Map
Used when you need to store values using a key.
Now let’s understand each one with a few examples.
List in Apex
A List stores multiple values in a specific order.
It can also contain duplicate values.
For example:
List<String> names = new List<String>();
names.add('John');
names.add('Sarah');
names.add('John');
The List contains:
John
Sarah
John
Notice that John appears twice.
That’s allowed because a List can contain duplicate values.
Accessing values from a List
Lists use an index to access individual values.
The index starts from 0.
System.debug(names[0]);
This returns:
John
And:
System.debug(names[1]);
returns:
Sarah
So if you have three values, their indexes will be 0, 1, and 2.
Some commonly used List methods
Here are a few List methods you’ll come across regularly:
names.add('Michael');
Adds a value to the List.
names.remove(1);
Removes the value at index 1.
names.size();
Returns the number of values in the List.
names.contains('John');
Checks whether the List contains a particular value.
You don’t need to memorize every List method when you’re starting out. These few are enough to get comfortable with the basics.
Lists and SOQL
Lists become especially useful when working with SOQL.
A SOQL query can return multiple records, and we commonly store those records in a List.
List<Account> accounts = [
SELECT Id, Name
FROM Account
LIMIT 10
];
Now accounts contains the Account records returned by the query.
We can then loop through them:
for (Account acc : accounts) {
System.debug(acc.Name);
}
You’ll see this pattern frequently when working with Apex.
Set in Apex
A Set is useful when you need unique values.
Let’s say you have a collection of Account IDs, but some of those IDs appear more than once.
For example:
001xx000001
001xx000002
001xx000001
001xx000003
If we only need the unique IDs, keeping duplicates doesn’t help much.
This is where a Set comes in.
Set<String> cities = new Set<String>();
cities.add('Jaipur');
cities.add('Delhi');
cities.add('Jaipur');
Even though Jaipur was added twice, the Set keeps only one occurrence.
So the Set contains:
Jaipur
Delhi
A common Salesforce example
Sets are often useful when collecting record IDs.
For example:
Set<Id> accountIds = new Set<Id>();
for (Contact con : contacts) {
accountIds.add(con.AccountId);
}
If several Contacts belong to the same Account, its ID will still appear only once in the Set.
This is particularly useful when working with SOQL.
For example:
List<Account> accounts = [
SELECT Id, Name
FROM Account
WHERE Id IN :accountIds
];
Instead of dealing with duplicate Account IDs, we first collect the unique IDs in a Set.
One thing to remember about Sets
Sets don’t work like Lists when it comes to order.
If the order of your values matters, a List is generally a better choice.
Map in Apex
A Map is a little different from List and Set.
Instead of simply storing a group of values, a Map stores data as key-value pairs.
You can think of it like this:
Account ID Account
001xx000001 → Acme
001xx000002 → Salesforce
001xx000003 → Global Media
The key helps us find the value.
In Apex, we can create a Map like this:
Map<Id, Account> accountMap = new Map<Id, Account>();
Here:
Idis the key.Accountis the value.
Adding values to a Map
For a simple example:
Map<String, String> employees = new Map<String, String>();
employees.put('E001', 'John');
employees.put('E002', 'Sarah');
employees.put('E003', 'David');
Now we can retrieve an employee using their key:
String employeeName = employees.get('E002');
The result is:
Sarah
This is one of the main reasons Maps are useful.
Instead of going through every value to find what we need, we can use the key.
Maps with Salesforce records
Maps become even more useful when working with Salesforce records.
For example:
Map<Id, Account> accountMap = new Map<Id, Account>(
[SELECT Id, Name FROM Account]
);
Now each Account is stored using its Id as the key.
We can retrieve an Account using its Id:
Account acc = accountMap.get(accountId);
This pattern is very common in Apex, especially when working with multiple records.
Checking whether a key exists
We can also check whether a Map contains a particular key:
if (accountMap.containsKey(accountId)) {
Account acc = accountMap.get(accountId);
}
containsKey() returns true when the key exists in the Map.
This can be useful when you aren’t sure whether a particular record is present.
Final Thoughts
Collections are one of those Apex concepts that look simple at first, but you’ll end up using them almost everywhere as you write more Apex.
The important part isn’t memorizing every method.
It’s understanding what each collection is meant to do:
List helps you work with an ordered group of values.
Set helps you work with unique values.
Map helps you find a value using a key.
Once these three concepts become familiar, working with SOQL results and other Apex code becomes much easier.
And the more Apex you write, the more often you’ll find yourself reaching for one of these three collections.
