Understanding Collections in Apex | Salesforce Developer’s Reference

INTRODUCTION:

Collection, in its most general sense, refers to a ‘group of items.’ Specifically, there are four main types of collections:

  1. List
  2. Set
  3. Map
  4. Array

However, our focus here is on the three types supported by Apex, which include List, Set, and Map.

1. List

It is a collection of similar elements.

    • Size of the List can grow or reduce based on the runtime requirement.
    • Elements in the list are referred using index.
    • Index value starts with “0”.
    • List maintains insertion order.
    • List will accept duplicate values.
    • Salesforce has predefined apex class called List.
    • This class has predefined methods to support the operations on the list.

The following two declarations are equivalent.

The Str variable is declared using the List syntax

List<String> Str= new List<String>();

Alternatively, you can declare the “Str” variable as an array but assign it to a list, rather than an array.

String[] Str = new List<String>();

This example illustrates both methods of adding elements to a list.

List<String> Str = new List<String> { 'red', 'green', 'blue' };
List<String> Str = new List<String>();
Str.add('orange');
Str.add('purple');

Here is an example that demonstrates how to iterate over the elements of an array.

String Str1 = Str .get(0);
String Str2 = Str [0];
// Iterate over a list to read elements.
for(Integer i=0;i<Str.size();i++) {
    // Write value to the debug log
    System.debug(Str[i]);
}

There are various methods used in lists-

add(),addAll(),clone(),clear(),get(),remove(),size(),isEmpty() etc.

2. Set

A set is an-unordered collection of objects that doesn’t contain any duplicate values.Use a set when you don’t need to keep track of the order of the elements in the collection,and when the elements are unique and don’t have to be sorted.

OR

  • It is a collection of similar elements.
  • Size of the Set can grow or reduce based on the runtime requirement.
  • Elements in the set are referred using value NOT INDEX.
  • Index value starts with “0”.
  • Set maintain insertion order.
  • Set will NOT accept duplicate values.
  • Salesforce has predefined apex class called Set.
  • This class has predefined methods to support the operations on the Set.
  • Set cannot be displayed in PageBlocktable.
  • We can display set records by declaring a set reference name/variable. Eg. {!a}

The following example creates and initializes a new set,add some element,and checks if the set contains the string b’:

Set<String> s = new Set<String>{'a','b','c'};
if (s.contains('b')) {
    System.debug ('I contain b and have size ' + s.size());
}

Methods used in Set are the same as used in List.

3. Map

Maps are collections of key value pairs,where the keys are of primitive data types. Use a map when you want to store values that are to be referenced through a key.For example,using a map you can store a list of addresses that correspond to employeeIDs.

OR

  • It is a collection of similar elements.
  • It is a key value pair.
  • The size of the map can grow or reduce dynamically based on run time requirement.
  • Key in the map should be unique.
  • Value in the map can be duplicated.
Map<Integer,String> empAdd = new Map<Integer,String>(); 
empAdd.put (1, '123 San Francisco, CA'); 
empAdd.put (2, '456 Dark Drive, San Francisco, CA'); 
System.debug('Address for employeeID 2: ' + empAdd.get(2));

Maps also support a shortcut syntax for populating the collection when creating it.

Map<String,String> myStrings = new Map<String,String>
{ 
    'a'=>'apple','b'=>'bee'
}; 
System.debug(myStrings.get('a'));