- SalesforceChaCha
- Posts
- π Understanding collections in Apex πΊ
π Understanding collections in Apex πΊ
Lists, Sets, and Maps ... oh my!
Good morning, Salesforce Nerds! We all know the golden rule of Salesforce development is bulkification.
But are we aware of what tools are available to a developer in order to adhere to this rule?
No sweat π. We got you.
Today weβre going to see what collections are in Apex, what behavior they give a dev, and see how to choose between them!

TABLE OF CONTENTS
Understanding collections in Apex
FIRST THINGS FIRST
What are collections?
Collections are fundamental constructs in Salesforce development that store and manage groups of data elements. πͺ
Developers use them to efficiently organize, access, and manipulate data in their Apex code.
Without collections, bulkification would not be possible!
Weβre going to get into the three types available and see how each type has its own set of properties and methods that define their behavior and usage. π°
Ready? Letβs go! ποΈ
COLLECTION TYPE #1
Lists
A List is an ordered collection of elements. β
Lists are beneficial when preserving the order of data or accessing elements based on their position is necessary. βοΈ
Think of it like a shopping list β each item is numbered and has a specific place in the list.
You can do lots of things with a list:
Add elements with the
add()methodAccess elements with the
get()methodUpdate elements with the
set()methodRemove elements with the
remove()methodSort elements with the
sort()method
COLLECTION TYPE #2
Sets
Sets are unordered collections that store only unique elements. β
These are useful when you need to ensure that your data is distinct and when you donβt need to maintain any order to the elements.
Kind of like a drawer full of different socks β each pair is unique, but thereβs no specific order to them. π§¦
What can we do with a Set?
Add elements with the
add()methodRemoving elements with the
remove()methodChecking if an element exists with the
contains()methodAccessing elements only by iterating over them
COLLECTION TYPE #3
Maps
Maps are a collection of key-value pairs. β
Use a Map when you need to find value(s) based on their key.
Just like a dictionary β each word is the key and they each have a corresponding definition as their value. π€―
So what do these give us?
Add a key-value pair with the
put()methodAccess a value for a specific key with the
get()methodUpdate a value for a specific key with the
get()method (yep - you can overwrite an existing value here)Remove a key-value pair with the
remove()method
WRAPPING THINGS UP
Comparing collections
Each collection type offers itβs own unique features. Itβs critical to understand how to choose the best option to meet you needs.
We got you though. Just check out this table. ποΈ
Type | Features | Use Case |
|---|---|---|
List | β Ordered collection of elements β Allows duplicates β Elements accessible by index | ποΈ When order matters ποΈ When duplicates are okay |
Set | β Unordered collection of elements π« No dups allowed π« Elements not accessible by index. Only by iterating over the collection. | ποΈ When order doesnβt matter ποΈ When duplicates are not okay |
Map | π Collection of key-value pairs π Keys are unique and used to access values β No order to the elements | ποΈ When mapping a key to a value ποΈ When accessing values by a unique key ποΈ When duplicate keys are not okay |
SOUL FOOD
Todayβs Principle
"The measure of intelligence is the ability to change."
and now....Salesforce Memes



What did you think about today's newsletter? |