Thursday 23 October 2014

Collections In Salesforce

                                                   Collections


Apex has the following types of collections:

*      Lists
*      Maps
*      Sets

Lists:

A list is an ordered collection of elements that are distinguished by their indices.

List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.

List will Allow duplicate values.

Syntax:

Syntax

List<datatype> listName = new List<datatype>();
  • The datatype allows both primitive datatypes and non-primitive datatypes
  • Size of the list is dynamically increased.

Sets
Set is an unordered collection of elements which will not allow duplicates.

Syntax

set<datatype> setName = new set<datatype>();
  • data type allows only primitive datatypes and SObjects.
  • We cannot get the retrieve the data based on index because set does not  have index.

Maps
Map is key – value pairs.

Syntax

map<datatype,datatype> mapName = new map<datatype,datatype>();
  • First datatype is key and it allows primitive datatypes and should be unique.
  • Second datatype is values and it allows both primitive & non-primitive datatypes and allows duplicates.


 

Methods in List

Add

Add the values to the list.
Ex: List<String> colors =new List<String>();
colors.add(‘Red’);
colors.add(‘White’);
colors.add(‘Black’);    (OR)   
List<String> colors =new List<String>{‘Red’,’White’,’Black’}

Get

Retrieve a values from list using index.
String getcolor = colors.get(1); —-  we get the value is ‘White’ in to getcolor veriable.

Set

Replaces a former value with the value at given index.
colors.set(1,’Green’); —-   List has value at index ‘ 1 ‘ is White is changed to Green.

Size

Return the number of elements in the list.
colors.size(); —-    Gives the size of colors list is ‘2’

Clear

Remove the elements from the list.
colors.clear();

 

---------------------------------------------------------------------------------------------------------------------------

Example:  Inserting 100 contacts in contacts object

public class ContactInsertion{
list<contact> contactList = new list<contact>();
public void methodName(){
for(integer i=0; i<100 ; i++ ){
contact cont = new contact(lastname = ‘contact’ + i);
contactList .add(cont);
}
insert contactList ;
}
}



Write in Anonymous block and excute it.
list<contact> contactList = new list<contact>();
for(integer i=0; i<100 ; i++ ){
contact cont = new contact(lastname = 'prasadl' + i);
    system.debug('names are : '+'prasadl'+i);
contactList .add(cont);
}
insert contactList ;

 Set Methods
set<string> myString = new Set<String>{'a', 'b'};
    myString.add('d');
system.debug('add method = ' +myString);
   mystring.remove('a');
system.debug('remove method = ' +mystring);
system.debug('contains method = ' +myString.contains('c'));
system.debug('isempty method = ' +mystring.isempty());
  set<string> myString1 = myString.clone();
system.debug('clone method = ' +mystring1);


Methods

Put()

Insert a key-value pair or replaces a value with the given value for the key .
  •    map.put(key,value);

Get()

Retrieves the value for a key.
  • map.get(key);

keySet()

Retrieves all the keys and return type is set;
  • map.keySet();

values()

Retrieves all the values and return type is list;
  • map.values();

Size()

Return the number of components in the map.
  • map.size();

Example: Create a map of Account Ids and Account objects.

public class AccountMapPopulation{
// Creating map with account id and account object
map <Id,Account> accountIdMap =new map<Id,Account>();
public void methodName(){
// creating the accounts
account acc1 = new account (name =’account1′ , industry = ‘ Banking’);
account acc2 =new account(name =’ account2′ , industry = ‘Agriculture’);
account acc3 = new account(name=’account3′ , industry=’Banking’);
// populating the map with account id and account object
accountIdMap .put(acc1.id,acc1);
accountIdMap .put(acc2.id,acc2);
accountIdMap .put(acc3.id,acc3);
}
}
Ex:
List<Account> ls = [select Id,Name from Account];
Map<Id, Account> m = new Map<Id, Account>(ls);
*       Insert new record to the map
account a = new account(name = 'basha');
m.put(a.id,a);
insert a;
system.debug('new record '+a);
*       Get Record From map
account name1 = m.get('0019000000yVODTAA4');
system.debug('specific value' +name1);
*       Print Ids in the map
system.debug('map ids ' +m.keyset());
*       print values in the map
system.debug('map ids and values' +m.values());
*       copy one map to another map
Map<Id, Account> m1 = new Map<Id, Account>(m);
system.debug('map ids and values' +m1.values());
*       Size of the map
system.debug('map elements size' +m.size());
*       Map is empty or not
system.debug('map is empty or not' +m.isempty());
*       Clear the map
m1.clear();
system.debug('map ids and values' +m1);

Map<Integer, Account> M = new Map<Integer, Account>();
*       Verify if the list of sObjectscontains Account tokens
system.debug('=======map=====' +M.getSObjectType());
System.assertEquals( M.getSObjectType(), Account.sObjectType);

                   



No comments:

Post a Comment

Complete Salesforce CPQ training Free videos

Salesforcestart:: We are excited to announce that our YouTube channel, Salesforcestart, is your one-stop-shop for all things Salesforce CPQ!...