The Apex Master Class

What Are Collections in Apex? (Master Class Ep. 16)

3 min read

Subscribe on YouTube

What Is A Collection?

Every variable we’ve made so far holds exactly one thing:

Code
Integer numberOfTacos = 12;

One variable, one value. Fine for a while. Now imagine you’ve queried 200 Accounts out of your org and you need to do something to each of them. Are you making 200 variables? account1, account2, account3

Please say no.

A collection is a variable that holds a bunch of other values. One variable, many things inside, and you can operate on all of them together. That’s the whole idea, and it’s the point where your code stops being toy examples and starts being able to do real work.

Apex gives you three:

1. List – ordered, allows duplicates
2. Set – unordered, no duplicates
3. Map – key/value pairs

Each of those gets its own episode next. Today is just the big picture.


The Angle Brackets

Collections look a bit odd the first time:

Apex
List<Integer> numbers = new List<Integer>();
Set<String> names      = new Set<String>();
Map<Id, Account> accountsById = new Map<Id, Account>();

Those angle brackets are how you say what’s allowed inside. List<Integer> is “a list of Integers.” Set<String> is “a set of Strings.” A Map needs two, because it stores pairs: Map<Id, Account> means “Ids pointing at Accounts.”

And Apex enforces it. Try to shove a String into a List<Integer> and it won’t compile. That’s strong typing earning its keep again.

Note the new keyword. Collections are non-primitive types, so you have to build them before you can use them. Forget the new and your list is null, and you already know how that story ends.

You can also fill one on the spot:

Apex
List<String> tacoTypes = new List<String>{ 'Crunchy', 'Soft', 'Doritos Locos' };

They Can Hold Anything

Collections aren’t limited to primitives. They’ll hold SObjects and your own classes just as happily:

Apex
List<Account> accounts   = new List<Account>();
List<Computer> computers = new List<Computer>();

That second one is a list of instances of the Computer class we built a few episodes back. Perfectly legal, and genuinely useful once you start building anything of real size.

You can even nest them. Map<Id, List<Contact>> is a real and extremely common structure: “for each Account Id, here are all its Contacts.” Looks alarming. Isn’t.


Why Collections Actually Matter In Salesforce

Here’s the part that makes collections more important in Apex than in most languages, and I want to flag it now even though we won’t do it properly for a while yet.

Remember governor limits from episode 2? Salesforce caps how many SOQL queries and DML statements you can run in one transaction. As of today that’s 100 queries and 150 DML statements. Blow past it and your transaction dies. No partial credit.

So this is a disaster:

Code
//NEVER DO THIS. 200 accounts = 200 updates = dead transaction.
for (Account acct : accountsToUpdate)
{
    update acct;
}

And this is the fix:

Code
//One DML statement. However many records. Everybody's happy.
update accountsToUpdate;

Collections are what make that possible. You gather everything up, then hit the database once.

This is called bulkification, and it is genuinely the single most important habit in Salesforce development. Every experienced Apex developer you meet has it burned into their brain, usually because they learned it the hard way at 2am. Collections are the tool that makes it work, which is why they matter here more than they might elsewhere.


What’s Next

So: collections hold many values in one variable, angle brackets say what goes inside, and in Salesforce they’re the thing that keeps you the right side of governor limits.

The Apex Developer Guide’s collections chapter is here if you want to read ahead.

In the next episode we start with Lists, which are the ones you’ll reach for most.

See you next time!


Get Coding With The Force Merch!!

We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.

Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!


Check Out More Coding With The Force Stuff!

If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!

Youtube
Patreon
Github
Facebook
Twitter
Instagram


Salesforce Development Books I Recommend

Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps

Good Non-SF Specific Development Books:

Clean Code
Clean Architecture