Apex Collections Cheat Sheet: List vs Set vs Map (with the Patterns You Actually Use)

Header image: Apex collections cheat sheet, List vs Set vs Map.

Hey friends,

Use a List when order matters or you're about to insert or update records. Use a Set when you need unique values, usually Ids for a WHERE Id IN :ids filter. Use a Map when you need to find something by key quickly, usually a record by its Id. Apex has only these three collections, and no arrays apart from lists.

These definitions come from Trailhead's Apex & .NET Basics and Apex Database units.

Comparison of Apex List, Set and Map: ordering, duplicates, what each is used for, and typical declarations such as List of Account, Set of Id and Map of Id to Account.

The whole cheat sheet in one table.

List

An ordered, indexed collection, like an array. The first element is index 0.

ListBasics.apex
1List<String> colors = new List<String>{ 'red', 'green', 'blue' };
2colors.add('yellow');
3String first = colors[0];

Query results come back as lists, and DML takes lists. That's why insert accts; on a list beats inserting inside a loop.

Set

An unordered collection with no duplicates. Trailhead's example: a set is commonly used to store Ids because they're always unique, and then used in a SOQL WHERE clause.

CollectIds.apex
1Set<Id> acctIds = new Set<Id>();
2for (Contact c : Trigger.new) { acctIds.add(c.AccountId); }
3List<Account> accts = [SELECT Id, Name FROM Account WHERE Id IN :acctIds];

Map

Key-value pairs. Keys are unique and each maps to one value. Trailhead's example maps Accounts to their Ids, which you can build straight from a query:

AccountMap.apex
1Map<Id, Account> accountMap = new Map<Id, Account>([SELECT Id, Name FROM Account]);
2Account a = accountMap.get(someId);

Cert Lab by Nick Frates

Practice collections the PD1 way

The Platform Developer Cert Lab covers Apex data types, collections, SOQL and bulk patterns with practice questions that explain every option.

Start the PD1 Cert Lab, $9.99 →

The pattern: Set to query, Map to look up, List to save

This is the shape of almost every bulk-safe trigger, and it's the heart of my trigger best practices:

  1. Loop the records once and collect the Ids you need in a Set<Id>.
  2. Run one query with WHERE Id IN :ids and put the results in a Map<Id, SObject>.
  3. Loop again, look each related record up with map.get(), and add changes to a List.
  4. One DML statement on the list, outside the loop.

One query and one DML, no matter how many records. That keeps you far from the governor limits.

Exam traps

  • Index out of bounds: reading list[0] on an empty query result throws an exception. Check isEmpty() first.
  • Sets don't keep order. Don't rely on iteration order.
  • Map.get on a missing key returns null, not an error. Check containsKey() or null.
  • Adding a duplicate to a Set silently does nothing.

Keep studying

Hope this helps!

Best,

Nick

Keep reading

See all posts →

Salesforce Saturdays

Join 1,032 Salesforce pros getting one 10-minute tip on Salesforce tech or career growth every Saturday.