- Collections are the group of similar datatypes.
- Apex has the following types of Collections.
- Lists
- Maps
- Sets
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)
- 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