Skip to main content
5 answers
  1. Mar 10, 2016, 7:20 AM
    Please check below post for map method in salesforce

    1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htm

    2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_collections_maps.htm

    ADD:-

    How to add List in Map in one go :-

     

    List<Account> ls = [select Id,Name from Account];

    Map<Id, Account> m = new Map<Id, Account>(ls);

    PutAll

    Map<String, String> map1 = new Map<String, String>();

    map1.put('Red','FF0000');

    Map<String, String> map2 = new Map<String, String>();

    map2.put('Blue','0000FF');

    // Add map1 entries to map2

    map2.putAll(map1);

    System.assertEquals(2, map2.size());

    List<Account> accts = new List<Account>();

    accts.add(new Account(Name='Account1'));

    accts.add(new Account(Name='Account2'));

    // Insert accounts so their IDs are populated.

    insert accts;

    Map<Id, Account> m = new Map<Id, Account>();

    // Add all the records to the map.

    m.putAll(accts);

    System.assertEquals(2, m.size());

    REmove :-

    Map<String, String> colorCodes = new Map<String, String>();

    colorCodes.put('Red', 'FF0000');

    colorCodes.put('Blue', '0000A0');

    String myColor = colorCodes.remove('Blue');

    String code2 = colorCodes.get('Blue');

    System.assertEquals(null, code2);

    REmove list

    Map<String, String> colorCodes = new Map<String, String>();

    colorCodes.put('Red', 'FF0000');

    colorCodes.put('Blue', '0000A0');

    List<String> colorCodeList = new List<String>();

    colorCodeList.add('Blue');

    for(String str: colorCodeList)

    {

    colorCodes.remove(str);

    }

    Let us know if this will help you

    Thanks

    Amit Chaudhary
0/9000