Data collections in Salesforce Apex

by | Sep 23, 2021 | Salesforce

Home » Salesforce » Data collections in Salesforce Apex


In Salesforce Apex, we have Data Collections that are similar to that of Java. In Salesforce development, we use a lot of Data Collections as it is very handy to store the information. Let’s deep dive into Data Collections now.

Data Collection

Data Collections are used to store the elements of the same data type. Here, we have different methods to store these data based on our requirements. In Apex, we have three different ways:  Lists, Sets and Maps. Let’s try each of them individually.

Types of Data Collection


A List in Apex is defined as a collection of elements in order and with indices, as you know that starts from 0. It is mostly used in development to store any data type.

Index 0Index 1Index 2Index 3



List<Datatype> Name = new List<Datatype>();


public class testLists {
        public static void testingLists(){
              List<Contact> impCont = new List<Contact>();
impCont = [SELECT Name, Phone FROM Contact LIMIT 2];



Now for the above example, the output will be like this,

List in Salesforce


Now, let’s try to understand the code and the output. In the code we have created a class testList and a method testingLists. Inside the method we created a “list” of Contact data type to store contact records retrived from the SOQL statement. Hence when you execute the code you get the output where you can see the records.


A Set in Apex is a collection of elements in a unordered way and it won’t allow duplicates. It is used when you want to store unique elements.




Set<datatype> Name = new Set<datatype>();


public class testSet {
        public static void testingSets(){
               Set<String> impSet = new Set<String>();



Now for the above example, the output will be like this,

Sets in Salesforce


Here, you can find the difference that we added 4 elements in the to the Set but in the output we got only 3. It is obvious that Set won’t allow duplicate values. That’s why it returned only three elements.


A Map in Salesforce is a collection of key-value pairs. Here, key’s are unique and maps to a single value. Within Maps, both the key and the value can be of any datatypes.

Country (Key)“India”“USA”“UK”“Russia”
Currency (Value)“INR”“USD”“POUND”“RUBLE”



public class testMap {
    public static void testingMaps(){
                Map<Integer, String> impMap = new Map<Integer, String>();
                impMap.put(1, 'water');
                 impMap.put(2, 'gas');
                 impMap.put(3, 'liquid');



Now for the above example the output will be like this,

Map in Salesforce


Here, the key datatype was Integer and Value datatype was String. As same we entered the key’s and values using “put” method. When you observe the output it clearly show the key and values in a in this formate “Key = Value”.

Paramaterized Typing

Apex is a Statically typed language like java. That means we have to specify every variable with a datatype in order to use it. If you observe all the above examples, you can clearly see every variable we created is specified with a data type.



Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.