Data collections in Salesforce Apex

by | Sep 23, 2021 | Salesforce

Home » Salesforce » Data collections in Salesforce Apex

Introduction

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

List

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.

India“USA”“UK”“Russia”
Index 0Index 1Index 2Index 3

 

Syntax:  

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

Example:

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

 

Output:

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

List in Salesforce

Explanation:

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.

Sets

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.

“India”“USA”“UK”“Russia”

 

Syntax:

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

Example:

public class testSet {
        public static void testingSets(){
               Set<String> impSet = new Set<String>();
                                impSet.add('mark');
                                impSet.add('john');   
                                impSet.add('katie');
                               impSet.add('mark');
                                System.debug(impSet);
    }
}

 

Output:

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

Sets in Salesforce

Explanation:

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.

Maps

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”

 

Example:

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');
                 System.debug(impMap);
    }
}

 

Output:

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

Map in Salesforce

Explanation:

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.

Reference

https://www.sfdc99.com/2013/09/28/data-collections-lists-sets-and-maps/

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections.htm

Author

0 Comments

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.

Author