Wednesday, September 12, 2018

Collection in salesforce

Collections in apex can be lists, sets, or maps. There is no limit on the number of items a collection can hold. However, there is a general limit on heap size.

1) List 
2) Set 
3) Map 

List:
  1.  List is an ordered collection of element.
  2.  Can store duplicate 
  3.  We can perform dml operations on list
Syntax:

To declare a list, use the List keyword followed by the primitive data, sObject, nested list, map, or set type within <> characters. For example:
  • list<object> listName=new list<object>();     // List of any object.
  • list<String> listName=new list<String>();    // List of string.
Structure of list is shown below:

Location:   Index 0   Index 1  Index 2   Index 3  Index 4
Values:          1                   2              3               4         3


Methods available with list are:

get(index)

It is used for accessing element from list.

add(listElement)

It is used for adding element to list.

size(); 

It is used for checking size of list.

clear();

It is used for clearing list.

clone();  

It is used for making a duplicate copy of a list.

contains(listElement)

It returns true if the list contains the specified element.

remove(index) 

It removes the list element stored at the specified index, returning the element that was removed.

sort()

Sorts the items in the list in ascending order.


Set:
  1. Set is unordered 
  2. Duplicates are not allowed in set  
Methods available with set are,
add();  // adding element to set...
remove(); // removing elemnt from set...
contains();  // checking element inside set...

Syntax: 
  • Set<id> idList=new Set<id>();   //set of id's
Adding element to set,
idList.add(1);

Removing added element from Set,

idList.remove(1);

Map==> Map is used to store key value pair. 

Map in salesforce



Method available with  map are, 


Keyset();  

Values(); 
get(Keyid); 
put(key,value); 
containskey(key); 
clear();
size();
clone();
isEmpty();
remove(key);

Syntax: 

Map can be used in different ways some listed below,
  • Map<string,string> mapName=new map<string,string>();  
  • Map<integer, string> mapName = new Map<integer, string>();
  • Map<integer, objectName> mapName = new Map<integer, objectName>();
  • Map<string,list<objectName> mapName= new 
  • Map<string,list<objectName> ();

Sample ex:

1)For storing contact in map with unique number.

       Map<integer, contact> mapName = new Map<integer, contact>(); 
       contact obj=[select id,name from contact limit 1];
       mapName.put(1,obj);

2)For storing Child record list related to parent record.      

      Map<String,list<contact>> accountWithContactsMap= new Map<String,list<contact>>();

      List<Account> accountList=new List<Account>();  

    accountList=[Select id,Name,(Select id,Name from Contacts) from Account];   

        for(Account obj:accountList)   
         {   
           if(obj.Contacts.size() >=1)      
            {        
              if(!accountWithContactsMap.containsKey(obj.id))  
               {       
                 accountWithContactsMap.put(obj.id,obj.Contacts); 
               }

            }

        }

To get all Contacts related to particular Account.

Account obj= new Account();
obj=[Select id,name from account limit 1];

List<Contact> conList1=new List<Contact> conList1();

Map<String,list<contact>> accountWithContactsMap1= new Map<String,list<contact>>();


  if(accountWithContactsMap.containsKey(obj.id))
    {
     conList1=accountWithContactsMap1.get(obj.id);

    }

2 comments:

  1. Nice article.
    How can we use clone and deepclone for standard datatypes in a Map?

    ReplyDelete