πŸ’ƒ 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

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() method

  • Access elements with the get() method

  • Update elements with the set() method

  • Remove elements with the remove() method

  • Sort elements with the sort() method

  • There’s more here …

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() method

  • Removing elements with the remove() method

  • Checking if an element exists with the contains() method

  • Accessing elements only by iterating over them

  • Again, more here …

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() method

  • Access a value for a specific key with the get() method

  • Update 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

  • Full list here …

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."

Albert Einstein

and now....Salesforce Memes

What did you think about today's newsletter?

Login or Subscribe to participate in polls.