13 December 2013

Explaining about Collections in Apex?

  • Collections are the group of similar datatypes.
  • Apex has the following types of Collections.
  1. Lists
  2. Maps
  3. Sets
Note : There is no limit on the number of items a collection can hold however there is a general limit on heap size.

1.Lists :
          A List is an ordered collection of typed primitives SObjects,user-defined objects,Apex objects or collections that are distinguished by their includes.
  • The index position of the first element in a list is always ' 0 '
  • To declare a list,use the list keyword followed by the primitive data,SObject,nested list,map or set type within < > characters.
  • List < string > mylist=new list< string > ();  (This will store the block of memory at the Database)
Note : 
   - A list can only contain up to five levels of nested collections inside it.
   - In a list,we can add duplicate values that is duplicate values will be allowed inside a list.

2.Sets :
         A set is an un-ordered collection of primitives or SObjects that do not contain any duplicate elements.
To Declare a set,use the set keyword followed by the primitive datatype name within < > characters.

Ex : set < string > s=new set < string > ();

3.Maps :
         A map is a collection of key-value pairs where each unique key maps to a single value.
-  Keys can be any primitive datatype while values can be primitive,sobject,collection type or Apex object.
- To declare a map,use the map keyword followed by the data types of the value within < > characters.

    Ex  : Map <string,string> m=new map<string,string>();

Note : Similar to lists,map values can contain any collection and can be nested within one another.A map can only contain up to five levels of nested collections inside it.


No comments:

Post a Comment