Collections In Apex

Introduction To Collections In Apex | Salesforce Developer Guide

INTRODUCTION:

Collection: In general means 'Group of things'.

Basically there are 4 types of collection which are as-

    • List
    • Set
    • Map
    • Array.

But here we are discussing only about three types which are supported by Apex(List,Set and Map).

dont miss out iconDon't forget to check out: Salesforce Apex Trigger - Parent to Child Trigger Using List

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, the Str variable can be declared as an array but assigned to a list rather than an array.

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

The example shows you both ways 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');

This is an example which shows how to iterate over array elements.

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}

dont miss out iconCheck out another amazing blog by Anuj here: How to Use Salesforce Lightning Data Services

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'));

References: WWW3, Programiz

Popular Salesforce Blogs