Salesforce Collections: A Concise Overview

Collections can be thought of as a type of variable capable of storing multiple records. For example, a List can hold multiple records of Account objects. Now, let’s delve into a comprehensive overview of all collection types.


A list is a sequentially organized assortment of elements identified by their respective indices. These elements may encompass a wide range of data types, including primitive types, collections, sObjects, user-defined types, and native Apex types. Lists have the flexibility to store various collections and can be structured in a nested manner, enabling them to become multidimensional.


List<string> ListOfCities = new List<string>();
System.debug('Value Of ListOfCities'+ListOfCities);

List Operations:

Various techniques are at your disposal when working with Lists to achieve various functionalities, such as calculating the List’s size or adding elements. The following are some of the most commonly used methods:

  1. size()
  2. add()
  3. get()
  4. clear()
  5. set()

While you can use the array notation to declare Lists, this practice is generally not common in Apex programming.

String [] ListOfStates = new List<string>();

Collections of Unique Elements

A Set is a collection type that comprises multiple unordered, distinct elements. Sets do not allow duplicate entries. Similar to Lists, Sets can also be nested.


Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};
System.debug('Value of ProductSet'+ProductSet);

Set Operations:

Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};
System.debug('Set with New Value '+ProductSet);

Key-Value Pair Collections

It is a pairing of unique keys with their respective values, where both the key and the value can take on any data type.


Map<string, string> ProductCodeToProductName = new Map<string, string>
{'1000'=>'HCL', '1001'=>'H2SO4'};