A Step-by-Step Guide to Collections in Apex (Lists, Sets and Maps)

When you start working with coding languages you start to realize that you need to store collections of information in variables instead of just one value at a time. In Salesforce you do this through Collections. If you’ve never used collections in Salesforce, I’d like to break down for you how I understand these and help you to get an idea of how collections are used and when you might use them.

Collections (Lists, Sets, Maps)

Apex uses collections to store multiple records. It’s basically just a way to collect records for use later on in your code. You will want to use those records in various ways and you have three different types of collections you can store the records in. Each method has it’s own benefits and drawbacks, but you will probably use them all, so it’s good to become familiar with them.

Working with Lists

A list is an ordered collection that can store records of primitive types (integer, string, etc), collection types (lists, sets, maps), sObjects (Account, Contact, etc), user defined types, and built-in Apex types. Lists CAN have duplicate records.

In the table above you can see a visual representation of a list of strings. The first element in a list is always at position zero (o). This was a list of strings, but a list can be a list of lists, and be multidimensional.

In order to use a list, you must declare it using the keyword List followed by the type of list you will be using.

Above you can see that I created a list of strings called “listOfCarMakes”. On line 2 I am printing out the values that are in the list to help with debugging. Currently this would not print out any values since we have only declared and instantiated the list, but not added any values to it.

Lets add 4 cars to the list as in our example above.

You can add values to the list using the above notation and then when you print out the debug value, you will see the 4 cars now in the list.

It’s also possible to add values to the list in a different way using less code. I’m all for using less code when possible because it usually makes things simpler and easier to understand.

The above code does will add the strings to the list in only three lines of code .

Working with Sets

A set is an unordered collection that can store records of any data type, just like lists. Sets however, CANNOT have duplicate records.

 You will want to use a set vs. a list when you need to make sure your collection does not have any duplicate elements. You can add elements to a set using the .add() method just like with lists. 

Working with Maps

A map is a collection of key value pairs which contain unique keys for each single value. Keys and values can be of any data type, just like lists and sets.

Below is a table that visually represents a map.

With maps you declare it by using the keyword Map followed by the data type of both the key and the value.

You can see that in order to add elements to a map you have to use the keyword put vs. add. Another different between maps vs. lists and sets is that you add both a key and a value, versus just adding one value to a list or set. 

Maps are VERY powerful and whenever I can I try to use maps instead of lists when it makes sense.


50% Complete

Get our latest tutorials, tips and tools to help you learn to be an Apex Programmer!

Every week I'll update you on the latest from Apex Coding Academy and you'll get first access to new resources, offers and events.