You need to sign in to do that
Don't have an account?
sfdc start
plz answer todays interview question..
1. plz give an example on account object using SET..
2.plz give an example on account object using MAP.
3.plz give an examle on account object using SET MAP & LIST...
2.plz give an example on account object using MAP.
3.plz give an examle on account object using SET MAP & LIST...
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DDpWIAW
There are to many example are given in developer guide. I will recomend you to download the Apex developer guide .
1) http://amitsalesforce.blogspot.in/search/label/Book
2) https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf
Maps
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm
A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Lists
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm
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
Set
A set is a collection of unique, unordered elements. It can contain primitive data types (String, Integer, Date, etc) or sObjects.
EX:
Account acct1 = new account(Name='ACME Corp', BillingAddress='100 Main');
Account acct2 = new account(Name='ACME Corp');
Set<Account> s = new Set<Account>{acct1, acct2};
***************************************************
There is a realtime situation where-in we need to loop through the collection of records and get the appropriate value from the matching record.
Requirement is:
Using trigger populate the Account Type on the Contact record (only insert scenario).
To achieve this requirement, here I am mentioning using trigger, so planning to write a trigger:
If we use List and not Map
Same Trigger using the Map:
If you compare both the triggers,
Trigger 1 is having a List of Accounts, where in we have to loop through the matching Account every time to populate the Acc_Type in the second for loop.
Trigger 2 is having a Map of Accounts with Id and Account as the Datatypes. Hence we can directly get the corresponding Account record and populate the Acc_Type easily.
Hope this will clear you actual doubt.
All Answers
SET:
A set is a collection of unique, unordered elements. It can contain primitive data types (String, Integer, Date, etc) or sObjects.
EX: Account acct1 = new account(Name='ACME Corp', BillingAddress='100 Main'); Account acct2 = new account(Name='ACME Corp'); Set<Account> s = new Set<Account>{acct1, acct2};
LIST:
Lists are the mostly widely used collection so learn to love them (I do!!). A List is a collection of primitives, user-defined objects, sObjects, Apex objects or other collections (can be multidimensional up to 5 levels).
EX: List<String> ids = new List<String>{'0017000000cBlbwAAC','0017000000cBXCWAA4'}; List<Account> accounts = [Select Name From Account Where Id IN :ids];
MAP:
A Map is a collection of key-value pairs. Keys can be any primitive data type while values can include primitives, Apex objects, sObjects and other collections.
EX: Map<Id,Account> accountMap = new Map<Id, Account>( [Select Id, Name From Account LIMIT 2]); System.debug('=== ' + accountMap); // keySet() returns a Set we can iterate through for (Id id : accountMap.keySet()) { System.debug('=== ' + accountMap.get(id).Name); }
P.S.: If it's helpful for you then Please mark as the best answer.
Nirdesh.
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DDpWIAW
There are to many example are given in developer guide. I will recomend you to download the Apex developer guide .
1) http://amitsalesforce.blogspot.in/search/label/Book
2) https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf
Maps
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm
A map is a collection of key-value pairs where each unique key maps to a single value. Keys and values can be any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Lists
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_lists.htm
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
Set
A set is a collection of unique, unordered elements. It can contain primitive data types (String, Integer, Date, etc) or sObjects.
EX:
Account acct1 = new account(Name='ACME Corp', BillingAddress='100 Main');
Account acct2 = new account(Name='ACME Corp');
Set<Account> s = new Set<Account>{acct1, acct2};
***************************************************
There is a realtime situation where-in we need to loop through the collection of records and get the appropriate value from the matching record.
Requirement is:
Using trigger populate the Account Type on the Contact record (only insert scenario).
To achieve this requirement, here I am mentioning using trigger, so planning to write a trigger:
If we use List and not Map
Same Trigger using the Map:
If you compare both the triggers,
Trigger 1 is having a List of Accounts, where in we have to loop through the matching Account every time to populate the Acc_Type in the second for loop.
Trigger 2 is having a Map of Accounts with Id and Account as the Datatypes. Hence we can directly get the corresponding Account record and populate the Acc_Type easily.
Hope this will clear you actual doubt.