Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Would it be possible to remove bunch of keys from a map in one single statement like this,

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

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

String myColor = colorCodes.remove('Blue','Red'); //this line throwing error
share|improve this question

1 Answer 1

up vote 6 down vote accepted

In your above example as per doc , Map's remove method takes in only one key as argument and returns a value as return type. you cannot pass a list of strings to the remove method, though I tried something like below based on java's removeall ( undocumented in salesforce map methods )

http://stackoverflow.com/questions/17675804/remove-multiple-keys-from-map-in-efficient-way

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_map.htm

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

colorCodes.put('Red', 'FF0000');
colorCodes.put('Blue', '0000A0');
colorCodes.put('Blue2', '0000A0');
set<string> remove_keys = new set<string>{'Red','Blue'};
    colorCodes.keySet().removeAll(remove_keys);
system.debug('#### Map after removing keys'+ colorCodes);

13:30:38.052 (52492111)|SYSTEM_METHOD_ENTRY|[9]|System.debug(ANY) 13:30:38.052 (52504469)|USER_DEBUG|[9]|DEBUG|#### Map after removing keys{Blue2=0000A0}

share|improve this answer
    
I did not know that existed, good find –  Phil Hawthorn Jul 29 at 20:35
    
Thanks @rao. I did not see it in the documentation. Somewhat missed from there. Thanks a lot. –  Baskaran S Jul 29 at 20:37
    
I try java syntax in apex and sometimes find hidden bounties like this :P –  rao Jul 29 at 20:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.