Sunday, May 1, 2016

COLLECTIONS IN APEX


Collection means a group of elements.We are familiar with arrays in c,c++ etc. its a classic example of collection.
But arrays can contain only single type of data.Often we have to form collection of elements of different types of data as in cases like defining properties of objects.In such cases arrays cant be used.We need more flexibility.This led to the formation of
versatile collections.Coming to Salesforce Apex,we have three  types of collections.
ie,
   1.Lists
   2.Sets
   3.Maps


 Let's describe those in detail

1.LISTS

   A list is an ordered collection of elements where each element is stored in specific location called index so that each element can be accessed by its index.So list is somewhat like array itself.When a list is created we have to specify its type.It can be anything from ID,String to  sObjects.



Syntax:-



  • To create an empty list of String


List<String> Lst = new List<String>();



  • To create a nested list


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


  • To define a new list

List<Integer> NList = new List<Integer>();



  • Adds a second element of value 27 to the end of the list
NList.add(27);                   

  • Retrieves the element at index 0                                

Integer i = NList.get(0);                   

  • Adds the integer 1 to the list at index 0
NList.set(0, 1);       


  •   Removes all elements from the list
              
NList.clear();                    









Sample Code:

trigger linkmyparent on child__c (after insert) {
  
   
   list<id> plist= new list<id>();

    
   for(child__c child:trigger.new)
   {
       
       plist.add(child.rltn__c);
        }

   list<parent__c> parent=[select Id,Count__c from parent__c where id in:plist];
    system.debug(parent);
   integer count=0;
    
   for(child__c c:trigger.new)
    {
        for(parent__c p:parent)
        {
            if(c.rltn__c==p.id)
           
           p.Count__c++;
        }
         
    }
   update parent;


}




2.SET


A set is similar to list except that its unordered. It does not contain duplicate values.
It can contain primary data types, like Integer,strings etc. It can also contain more complex data types, such as sObjects. Adding elements to the set will dynamically increase the size of the set.



Syntax:



  •  Defines a new set with two elements

             Set<string> stringset = new Set<String>{'p', 'q + r'};


  • Defines a new set that contains the elements of the set created in the previous step

             Set<String> s2 = new Set<String>(s1);



Sample Code:


Set<String> parentid= new Set<String>();

for(Parent__c p:[select id from Parent__c ])
{
    parentid.add(p.id);

  }




3.MAPS


Maps  are similar to hash tables.Maps represent collection of key value pairs.
They are  organized based on the key. It uses the key to access the elements in the collection.
Keys and values in maps can be any data type—primitive types, collections, sObjects, user-defined types
Map is used when you need to access elements by using key.



Syntax:



  •   Define a new map with key Id and value string
               Map<ID, String> maplst = new Map<ID, String>();


  •  Define a new map with key Id and value list of strings


               Map<ID, list<String>> maplst = new Map<ID, list<String>>();


  •  Define a new map with key Integer and value string and populating the map with values


                Map<Integer, String> color = new Map<Integer, String>();
                m.put(1, 'Red');                  
                m.put(2, 'Blue'); 


  •  Define a new map with key Id and value sObjects


                Map<Id, sObject> m = new Map<Id, sObject >();


  •  Define a new map with key string and value string


                Map<String, String> c = new Map<String, String>();




Sample Code:

Map<Id, List<Traineeobj__c >> mapChild  = new Map<Id, List<Traineeobj__c >>();

 List<id> parentid=new list<id>();  
    
    for(Traineeobj__c t:trigger.new)
    {
        parentid.add(t.parent__c);
    }

    for(Traineeobj__c t :[select id, name, parent__c, Status__c FROM Traineeobj__c WHERE parent__c in :parentid])
    {
        if(mapChild.containsKey(t.parent__c))
        {
            mapChild.get(t.parent__c).add(t);
        }
        
        else
        {
            mapChild.put(t.parent__c, new List<Traineeobj__c> {t});
        }

    }

No comments: