Experience Salesforce

Collections in APEX

What You’ll Learn

  • Types of Collections in Apex

Apex language provides developers with three classes (Set, List, Map) that make it easier to handle the collection of objects. In a sense, these collections work somewhat like arrays except their size can change dynamically and they have more advanced behaviors and easier access methods than arrays.

Types of Collections in Apex

There are three main types of collections in Apex:

1. Lists

A list is an ordered group of items identified by their indices. List elements can be created using any data type, including primitive types, collections, sObjects, user-defined types, and integrated Apex types.

Lists are multidimensional and can contain any kind of collection. They can also be nested inside other lists. One may have, for instance, a list of lists of integer sets. A list can have up to eight layers total or up to seven levels of nested collections inside of it.

2. Sets 

An unordered collection of unique elements without any duplicates is called a set. The element of Set could be any data type, including primitive types, collections, sObjects, user-defined types, and integrated Apex kinds.

Collections within sets have the ability to be nested inside one another. You may have a set of lists of lists of integer sets, for instance. A set can have up to eight levels total or up to seven levels of nested collections inside of it.

3. Maps 

A map is an assembly of key-value pairings in which every distinct key corresponds to a single value. Any data type, including primitive types, collections, sObjects, user-defined types, and integrated Apex types, can be used for keys and values.

Map keys and values can contain any collection, including nested collections. One way to organize data is to create a map from integers to mappings, which then maps from strings to lists. Up to seven nested collection layers, or a maximum of eight levels total, may be present in map keys.

salesforce marketing cloud CTA
Next Topic

Need Extra Support? Our FREE study materials have got you covered.

Our expert-prepared study materials provide the answers you need. Clear your doubts and improve your skills with detailed notes from industry professionals.

cts-img