Collections in Apex (List, Set, Maps)

In every programming language, there are collections. The collection could be considered as a type of variable that can store multiple records. For example, List is a collection, which can store several Account object's records. The list is not only a collection variable in Apex programming language, but it is also there in the other programming languages as well. In this chapter, we will discuss collections in Apex.

Lists

The list is capable of holding any number of records of the following type.

  • Primitive
  • Collections
  • sObjects
  • User-defined types
  • Built-in Apex type.

This is considered the most important type of collection in Apex. Apex has some system methods which have been created specifically to use with List. The list index always starts with 0. This is similar to the array in Java. A list should be declared with the keyword 'List'.

The example below shows, the list which contains the List of a primitive data type (string), which resembles a collection of cities.

List<string> ListOfCities = new List<string>();

System.debug('Value Of ListOfCities'+ListOfCities);

We can also declare the nested List in Apex. We can have up to five levels. This is called the Multidimensional list. For example, a list of the set of integers.

List<List<Set<Integer>>> myNestedList = new List<List<Set<Integer>>>();

System.debug('value myNestedList'+myNestedList);

Apex supports multiple methods that can be used for programming with List. Some of these methods are listed below.

  • size()
  • add()
  • get()
  • clear()
  • set()

The example below demonstrates the usage of these methods.

// Initialize the List
List<string> ListOfStatesMethod = new List<string>();

// This statement would give null as output in Debug logs
System.debug('Value of List'+ ListOfStatesMethod);

// Add an element to the list using the add method
ListOfStatesMethod.add('New York');
ListOfStatesMethod.add('Ohio');

// This statement would give New York and Ohio as output in Debug logs
System.debug('Value of List with new States'+ ListOfStatesMethod);

// Get the element at the index 0
String StateAtFirstPosition = ListOfStatesMethod.get(0);

// This statement would give New York as output in Debug log
System.debug('Value of List at First Position'+ StateAtFirstPosition);

// set the element at 1 position
ListOfStatesMethod.set(0, 'LA');

// This statement would give output in Debug log
System.debug('Value of List with element set at First Position' + ListOfStatesMethod[0]);

// Remove all the elements in List
ListOfStatesMethod.clear();

// This statement would give output in Debug log
System.debug('Value of List'+ ListOfStatesMethod);

Set

A Set is a collection type in Apex, that contains multiple numbers of unordered unique records. One of the unique features of Set is, it cannot have duplicate records. Like Lists, Sets can be nested as well.

The example below shows, how Set can be declared.

Set<string> ProductSet = new Set<string>{'Phenol', 'Benzene', 'H2SO4'};

System.debug('Value of ProductSet'+ProductSet);

Maps

A map is a key-value pair that contains the unique key for each value. Both keys and values can be of any data type.

The following example shows, how a product name is mapped with the product code.

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

// This statement would give as output as key-value pair in Debug log
System.debug('value of ProductCodeToProductName'+ProductCodeToProductName);

Map values are unordered and as a programmer, one should not rely on the order in which the values are stored. We should rather try to access the map always using keys. The map value can be null. Map keys when declared String are case-sensitive