Thursday, 31 December 2015

APEX- Collections


Collections somewhat works like arrays, except their size can change dynamically. In other way, you can say that it is much more advanced than arrays.
In simple words, these are type of variables which can store multiple number of records.
We often use collections because they go hand-in-hand with SOQL.

Apex has the following types of collections:
  • List
  • Set
  • Map

  • List is an ordered collection of elements
  • It will allow duplicates.
  • These are zero-based so the first element in the List is always 0.   
  • Datatype allows both Primitive datatype and non-primitive datatypes.
  • Size of the List increased dynamically.
     Syntax:  Basic Syntax of List are:
                       List <datatype> list_name = new List<datatype>();
                       List <datatype> list_name = new List<datatype>{value [, value2. . .]};

  • Set is an unordered collection of elements.
  • It will not allow duplicates.
  • It is typically used to store collection IDs that you want to use in SOQL query.
  • Datatype allows only Primitive datatype and sObjects.
Syntax:  Basic Syntax of Set are:
                    Set<datatype> set_name = new Set<datatype>(); 
                    Set<datatype> set_name = new Set<datatype>{value [, value2. . .] }; 

  • Map is a collection of Key –values pairs.
  • Key must be unique but values are duplicates.
  • Use a map when you quickly find something by a key.
  • Keys allows only Primitive datatypes and are unique only.
  • Values allows both Primitive and Non-Primitive datatypes.
    Syntax:  Basic syntax for Map are:

               Map<key_datatype, value_datatype> map_name = new map<key_datatype, value_datatype>();

               Map<key_datatype, value_datatype> map_name = new map<key_datatype, value_datatype>{key1_value =>  value1_value [, key2_value =>  value2_value. . .]};

Hope this helps you. 

No comments:

Post a Comment