function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdc startsfdc 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...
Best Answer chosen by sfdc start
Amit Chaudhary 8Amit Chaudhary 8
Please check below post
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

User-added image

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.
Map<Integer, String> m = new Map<Integer, String>(); // Define a new map
m.put(1, 'First entry');                  // Insert a new key-value pair in the map
m.put(2, 'Second entry');                  // Insert a new key-value pair in the map
System.assert(m.containsKey(1));  // Assert that the map contains a key
String value = m.get(2);               // Retrieve a value, given a particular key
System.assertEquals('Second entry', value);
Set<Integer> s = m.keySet();       // Return a set that contains all of the keys in the maListsp

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
List<Integer> myList = new List<Integer>(); // Define a new list
myList.add(47);                    // Adds a second element of value 47 to the end 
                                       // of the list
Integer i = myList.get(0);                   // Retrieves the element at index 0
myList.set(0, 1);                           // Adds the integer 1 to the list at index 0
myList.clear();                    // Removes all elements from the list

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
trigger ContactTriggerWithList on Contact (before insert) {
    // Here taking the Set because we don't need to maintain duplicate
    Set<Id> accIdSet = new Set<Id>();
    
    for(Contact con: Trigger.new) {
        if(con.AccountId != null) {
            accIdSet.add(con.AccountId);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        List<Account> accList = [Select Id, Name, Type from Account where Id IN: accIdSet];
        
        for(Contact con: Trigger.new) {
            if(con.AccountId != null) {
                for(Account acc: accList) {
                    if(con.AccountId == acc.Id) {
                        con.Acc_Type__c = acc.Type;
                    }
                }
            }
        }
    }
}

Same Trigger using the Map:
 
trigger ContactTriggerWithMap on Contact (before insert) {
    // Here taking the Set because we don't need to maintain duplicate
    Set<Id> accIdSet = new Set<Id>();
    
    for(Contact con: Trigger.new) {
        if(con.AccountId != null) {
            accIdSet.add(con.AccountId);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        Map<Id, Account> accMap = new Map<Id, Account>([Select Id, Name, Type from Account where Id IN: accIdSet]);
        
        for(Contact con: Trigger.new) {
            if(con.AccountId != null) {
                con.Acc_Type__c = accMap.get(con.AccountId).Type;
            }
        }
    }
}
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

Nirdesh_BhattNirdesh_Bhatt
Hi,

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.
 
Thanks,
Nirdesh.
 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post
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

User-added image

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.
Map<Integer, String> m = new Map<Integer, String>(); // Define a new map
m.put(1, 'First entry');                  // Insert a new key-value pair in the map
m.put(2, 'Second entry');                  // Insert a new key-value pair in the map
System.assert(m.containsKey(1));  // Assert that the map contains a key
String value = m.get(2);               // Retrieve a value, given a particular key
System.assertEquals('Second entry', value);
Set<Integer> s = m.keySet();       // Return a set that contains all of the keys in the maListsp

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
List<Integer> myList = new List<Integer>(); // Define a new list
myList.add(47);                    // Adds a second element of value 47 to the end 
                                       // of the list
Integer i = myList.get(0);                   // Retrieves the element at index 0
myList.set(0, 1);                           // Adds the integer 1 to the list at index 0
myList.clear();                    // Removes all elements from the list

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
trigger ContactTriggerWithList on Contact (before insert) {
    // Here taking the Set because we don't need to maintain duplicate
    Set<Id> accIdSet = new Set<Id>();
    
    for(Contact con: Trigger.new) {
        if(con.AccountId != null) {
            accIdSet.add(con.AccountId);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        List<Account> accList = [Select Id, Name, Type from Account where Id IN: accIdSet];
        
        for(Contact con: Trigger.new) {
            if(con.AccountId != null) {
                for(Account acc: accList) {
                    if(con.AccountId == acc.Id) {
                        con.Acc_Type__c = acc.Type;
                    }
                }
            }
        }
    }
}

Same Trigger using the Map:
 
trigger ContactTriggerWithMap on Contact (before insert) {
    // Here taking the Set because we don't need to maintain duplicate
    Set<Id> accIdSet = new Set<Id>();
    
    for(Contact con: Trigger.new) {
        if(con.AccountId != null) {
            accIdSet.add(con.AccountId);
        }
    }
    
    if(!accIdSet.isEmpty()) {
        Map<Id, Account> accMap = new Map<Id, Account>([Select Id, Name, Type from Account where Id IN: accIdSet]);
        
        for(Contact con: Trigger.new) {
            if(con.AccountId != null) {
                con.Acc_Type__c = accMap.get(con.AccountId).Type;
            }
        }
    }
}
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.





 
This was selected as the best answer