Collections in Apex (List, Set, and Map) are simply ways to store and manipulate multiple values in a single variable.
In day‑to‑day Salesforce development, you use them all the time in SOQL queries, triggers, and batch jobs to stay within governor limits and keep your code clean.
While working as a Salesforce developer, I was tasked with building an Apex class to automate a customer support process.
The requirement was to retrieve all open cases for a specific customer to see if their status was “New” or “In Progress.”
In Salesforce Apex, we use collections to store multiple records or values. Here, I will explain how to use collections in Salesforce Apex, the different types of collections, and demonstrate methods for manipulating collections.
What is a Collection in Apex?
Collections in Salesforce Apex are used to store multiple records or values in a single variable. Instead of creating many separate variables, collections help you manage data easily and write cleaner code.
They are very useful when working with SOQL queries, loops, and bulk data processing in Salesforce.
There are three main collection types in Apex: List, Set, and Map. A List stores data in an ordered way and allows duplicate values.
A Set stores only unique values and does not allow duplicates. A Map stores data as key–value pairs, where each key is unique and is used to access its related value.
Lists are commonly used when you want to process records one by one in a specific order. Sets are useful for removing duplic