๐Ÿ’ƒ 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.