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 8 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 )



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'};
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 '14 at 20:35
Thanks @rao. I did not see it in the documentation. Somewhat missed from there. Thanks a lot. –  Baskaran S Jul 29 '14 at 20:37
I try java syntax in apex and sometimes find hidden bounties like this :P –  Rao Jul 29 '14 at 20:37
This is actually documented method on Set: salesforce.com/us/developer/docs/dbcom_apex230/Content/… KeySet - is a set, and it has removeAll method. –  Patlatus May 29 at 13:42

Your Answer


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.