- 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? |